php - mysql_query() expects parameter 2 to be resource, boolean given, on line 9 -
this question has answer here:
i have code in 4 files.
the first 1 main page:
<?php session_start(); $pagetitle = "schedules"; include_once('head.php'); include_once('navbar.php'); ?> <body> name:<input type="text" id="depid" /> <input type="submit" id="depid-submit" value="search flight" /> <div id="depid-data"></div> <script src="jquery-1.8.2.min.js"></script> <script src="js/global.js"></script> </body> <?php include_once('footer.php'); include_once('scripts.php'); ?>
the 2nd part of code php coding:
<?php if( isset($_post['depid']) === true && empty($_post['depid']) === false) { require'../db/connect.php'; $query = mysql_query("select * flights depid ='depid'"); $result = mysql_query($query, $mysql_connect) or die(mysql_error()); echo "<table><tr><th>flight id</th><th>departure airport</th><th>arrival airport</th><th>distance</th></tr>"; while($row = mysql_fetch_array($result)) { echo "<tr><td>" . $row['flid'] . "</td><td>" . $row['depid'] . "</td><td>" . $row['arrid'] . "</td><td>" . $row['distance'] . "</td></tr>"; } echo "</table>"; } ?>
the third part of code connect database:
<?php $link = mysql_connect('localhost', 'ak118043_ako', '2391990ak4726790'); if (!$link) { die('not connected : ' . mysql_error()); } // make foo current db $db_selected = mysql_select_db('ak118043_project', $link); if (!$db_selected) { die ('can\'t use ak118043_project : ' . mysql_error()); } ?>
and 4th 1 ajax coding:
$('input#depid-submit').on('click', function(){ var depid= $('input#depid').val(); if($.trim(depid) != ''){ $.post('ajax/name.php', {depid: depid}, function(data){ $('div#depid-data').text(data); }); } });
the problem following error message:
warning: mysql_query() expects parameter 1 string, resource given in /home/ak118043/public_html/ajax/name.php on line 9
firstly, you're querying twice , using mysql_error()
has shown syntax error.
the error lies inside these 2 lines:
$query = mysql_query("select * flights depid ='depid'"); $result = mysql_query($mysql_connect,$query)or die ("error");
remove mysql_query()
in second query line being line 9 state:
$result = ($mysql_connect,$query) or die ("error");
but line $mysql_connect
seems connection variable , should come after , not first, mysqli_
syntax.
$result = ($query, $mysql_connect) or die ("error");
edit: after seeing connection code
this incorrect.
mysql_connect("localhost","ak118043_ako", "2391990ak4726790", "ak118043_project");
you're using 4 parameters should using three. 4 parameters used in mysqli_
, not mysql_
.
follow example php.net http://php.net/manual/en/function.mysql-select-db.php , replace own credentials:
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('not connected : ' . mysql_error()); } // make foo current db $db_selected = mysql_select_db('foo', $link); if (!$db_selected) { die ('can\'t use foo : ' . mysql_error()); }
because $mysql_connect
undefined , have no variable it.
now, instead of or die ("error")
or die(mysql_error())
, real error.
- consider moving
mysqli
prepared statements, or pdo prepared statements, they're safer.
also if( isset($_post['depid']) === true && empty($_post['depid']) ===false){
can replace if(!empty($_post['depid'])){
edit #2 (quick mysqli rewrite going)
change entire code following:
<?php $db_host = 'localhost'; $db_user = 'xxx'; // change own $db_pass = 'xxx'; // change own $db_name = 'your_database'; // change own $link = new mysqli($db_host, $db_user, $db_pass, $db_name); if($link->connect_errno > 0) { die('connection failed [' . $link->connect_error . ']'); } ?>
then
<?php if(!empty($_post['depid'])){ require'../db/connect.php'; $query = "select * flights depid ='depid'"; $result = mysqli_query($link, $query) or die(mysqli_error($link)); echo "<table><tr><th>flight id</th><th>departure airport</th><th>arrival airport</th><th>distance</th></tr>"; while($row = mysqli_fetch_array($result)) { echo "<tr><td>" . $row['flid'] . "</td><td>" . $row['depid'] . "</td><td>" . $row['arrid'] . "</td><td>" . $row['distance'] . "</td></tr>"; } echo "</table>"; }
saving face
- as per edit
$result = mysql_query($query, $mysql_connect)
you're using wrong variable.$mysql_connect
should$link
Comments
Post a Comment