Javascript and PHPMySQL
<?php
$host = 'localhost';
$dbusername ='root';
$dbpassword ='';
$database ='mp19';
$link =mysql_connect('localhost', 'root','12345678');
$db_selected = mysql_select_db('mp19', $link);
if (!$link)
{exit("Connection Failed: " . $link);}
$sql="(SELECT * FROM 3-33)";
$result=mysql_query($sql, $link);
if (!$result)
{exit("Error in SQL");}
echo "<table><tr>";
echo "<th>Date</th>";
while ($row=mysql_fetch_assoc($result))
{
var_dump($row);
echo "<td>$row['date']</td><开发者_如何学Go/tr>";
}
mysql_close($link);
?>
The connection is successful but the data doesn't get fetched from phpmysql and neither does it print in a table. Why is this so?
I think you made a few errors. It seems you lack a basic understanding of how these functions work. Almost all of them return some variable (like a link resource or a row array) that you can use in the rest of your code. If a call fails, these functions return false
. So you should check if the value if explicitly false
(by using the ===
operator) and only continue if everything is ok. Then, you must use the result value of the function, not the function itself. mysql_query is merely the call, not the result.
<?php
$host = 'localhost';
$dbusername ='root';
$dbpassword ='';
$database ='mp19';
$link = mysql_connect('localhost', 'root','12345678');
if ($link === false)
{
exit("Connection Failed: " . mysql_error());
}
if (mysql_select_db('mp19', $link) === false)
{
exit("Could not select database mp19: " . mysql_error());
}
// Is '3-33' really your table name?! You should pick another name.
$sql = "(SELECT * FROM 3-33)";
$resultset = mysql_query($sql, $link);
// (!$sql)? $sql SQL is always set here. It is just the string
if ($resultset === false)
{
exit("Error in SQL: " . mysql_error());
}
echo "<table><tr>";
echo "<th>Date</th>";
while ($row = mysql_fetch_assoc($resultset))
{
$date = $row['fieldname'];
echo "<td>$date</td></tr>";
}
mysql_close($link);
?>
The query is invalid - try loading your mysql client at the command line and running it:
mysql -uroot mp19 -p
(SELECT * FROM 3-33)
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '3-33' at line 1
精彩评论