Question:
PHP file unable to connect to MySQL database.?
?
2014-08-13 02:41:57 UTC
I am trying to connect my php file to a mysql database so I can draw data directly and load it onto the web page.
here is the php code so far:
echo "";
echo "";

echo "";

echo "

";
$test = "testdata!!!!";


echo "

This is some: $test

";
//connection to the database
$dbcnx = mysql_connect("localhost", "user", "password")
or die("Unable to connect to MySQL");
echo "Connected to MySQL
";

//select a database to work with
$selected = mysql_select_db("udp",$dbcnx)
or die("Could not select examples");

//execute the SQL query and return records
$result = mysql_query("SELECT packetdata FROM data");

//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo "Data:".$row{'packetdata'} //display the results
};
//close the connection
mysql_close($dbhandle);
echo "echo "";
?>

When I run this, the webpage remains completely blank. I'm new to php, is this a syntax error, or am I misunderstanding a principle here?

Three answers:
Kate
2014-08-13 03:04:49 UTC
A few things about this;



I can't see the start (


Why does the heading echo look like that? I think you missed the < on the



Finally - generally this would produce a syntax error, and normally I would set the login details for the database as variables and reference them in the connection - but not doing so wouldn't necessarily cause a problem unless they're incorrect.



In general I think your problem lies in the use of the ". switch your variables to use ' instead, particularly where you echo your data results. Like so;

echo "Data: '$row{'packetdata'}'";

(It doesn't look like you ended this echo either, which could have been another problem)

(EDIT: I also noticed you use { and }, normally I think I would use things like this with [ and ] eg. ['packetdata'], but as I've never tried to fetch data from a database with this particular method I'm not sure if this is intentional?)



If you have any more trouble drop me a dm or comment on here and I'll try to help.
anonymous
2014-08-13 02:54:48 UTC
No opening < on the end of

. Do you have a

?
2014-08-13 03:09:38 UTC
Made the changes you suggested, there was a





echo "";

echo "";



echo "";



echo "

";

$test = "testdata!!!!";



$dbcnx = mysqli_connect("localhost", "root", "")

or die("Unable to connect to database");

echo "Connected to MySQL
";



$select = mysqli_select_db("udp", $dbcnx)

or die("Could not select examples");



$result = mysql_query("SELECT * FROM data;");



while ($row = mysqli_fetch_array($result)) {

echo "Data:" ,$row[$result]

}



mysqli_close($dbcnx)

echo "

This is some: $test

";



echo "
echo "";

?>



This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...