Jump to content

Beginner question: PHP code does not retrieve data from MySQL.


Recommended Posts

Hi!

 

Today is the first day i try to program using PHP. I have downloaded the last XAMPP version, and created a simple database for testing.

 

Now i wanted to write a php piece of code to retrieve the data I have placed in the database, so after looking different tutorial pages, i have this:

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
</head>

<body>

<?php

$username="root";
$password="aaaa";
$database="marc_book";


$con = mysql_connect("localhost",$username,$password);
if (!$con)
  {
     die('Could not connect: ' . mysql_error());
  }

mysql_select_db($database);

$result = mysql_query("SELECT * FROM book");
$num = mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

   $isbn=mysql_result($result,$i,"isbn");
   $title=mysql_result($result,$i,"title");
   $pages=mysql_result($result,$i,"page_count");

   echo "$isbn $title $pages";

  $i++;
}
?>

</body>
</html>

 

When I run it, I get nothing. I hope a more skilled php-programmer will be able to detect what's wrong.

 

Thank you!!

 

Marc

 

Hi ignace. Thanks for the suggestion.

 

I have added the underscore in mysql_num_rows, but I continue getting the same blank page in the browser.

 

 

The database has been created using phpMyAdmin. The path to the database (in my code is set to "localhost") is correct? Or should I add anything else?

 

 

Thanks again!

mysql_numrows is an old depreciated alias for mysql_num_rows and won't prevent your code from working.

 

When you do a "view source" in your browser of the blank page, what do you get?

 

Did you add the two lines of code that  ignace suggested that would show all php runtime errors?

 

Hello!

 

 

 

When you do a "view source" in your browser of the blank page, what do you get?

 

 

I get the HTML code, which I believe is the same from the original html file:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
</head>

<body>



<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);


$username="root";
$password="aaaa";
$database="marc_book";


$con = mysql_connect("localhost",$username,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }


mysql_select_db($database);

$result = mysql_query("SELECT * FROM book");
$num = mysql_num_rows($result);




mysql_close();


$i=0;
while ($i < $num) {

$isbn=mysql_result($result,$i,"isbn");
$title=mysql_result($result,$i,"title");
$pages=mysql_result($result,$i,"page_count");


echo "$isbn $title $pages";

$i++;
}
?>


</body>
</html>

 

 

Did you add the two lines of code that  ignace suggested that would show all php runtime errors?

Yes, i did!

 

 

Thanks PFMaBiSmAd!

If the raw php code is present in the "view source" then either php is not functioning on your web server or the file extension is not .php or you browsed directly to the file instead of browsing to it through a URL.

 

What URL are you using in your browser? It should be something like http://localhost/yourfile.php

If the raw php code is present in the "view source" then either php is not functioning on your web server or the file extension is not .php or you browsed directly to the file instead of browsing to it through a URL.

 

What URL are you using in your browser? It should be something like http://localhost/yourfile.php

Yes, the error was there. The extension of the file was .html, instead of .php. Now it works properly.

 

 

Thanks a lot, it would have been frustrating not succeeding in an example as easy as this one.

 

Marc

I asked the same question ...

 

here is my source code so you can take a look and take anything you need:

<?
$fn = $_POST["fn"];
$ln = $_POST['ln'];
$ff = $_POST['ff'];
$fh = $_POST['fh'];
$yb = $_POST['yb'];
$mar = $_POST['mar'];
$dbc = mysqli_connect('mysql5.000webhost.com', 'a3393387_c2use', '******', 'a3393387_c2db')
or die('Cant connect to MySQL servies~!!');

$query = "INSERT INTO testing (fn,ln, " . "
ff, fh, yb, mar) " . "
VALUES ('$fn', '$ln', '$ff', '$fh', '$yb', '$mar')";

$result = mysqli_query($dbc, $query)
or die('error querying database');




mysql_connect('mysql5.000webhost.com', 'a3393387_c2use', '******') or die('error conecint');
echo "Connected to mysql~! <br />";
mysql_select_db('a3393387_c2db');
echo "Connected to database~! <br />";
echo "<b>Displaying details...</b> <br /><br />";
$query1 = "SELECT * FROM testing";
$result1 = mysql_query($query1) or die('cant select tables');
while($row = mysql_fetch_array($result1)) {
echo $row['fn'] ."-". $row['ln'] ."-". $row['ff'] ."-". $row['fh'] ."-". $row['yb'] ."-". $row['mar'];
echo "<br />";
}





mysqli_close($dbc);
?>

<a href="custom2.html">go back to steve's info form</a>

 

http://testo.comoj.com/custom2.php

I asked the same question ...

 

here is my source code so you can take a look and take anything you need:

<?
$fn = $_POST["fn"];
$ln = $_POST['ln'];
$ff = $_POST['ff'];
$fh = $_POST['fh'];
$yb = $_POST['yb'];
$mar = $_POST['mar'];
$dbc = mysqli_connect('mysql5.000webhost.com', 'a3393387_c2use', '******', 'a3393387_c2db')
or die('Cant connect to MySQL servies~!!');

$query = "INSERT INTO testing (fn,ln, " . "
ff, fh, yb, mar) " . "
VALUES ('$fn', '$ln', '$ff', '$fh', '$yb', '$mar')";

$result = mysqli_query($dbc, $query)
or die('error querying database');




mysql_connect('mysql5.000webhost.com', 'a3393387_c2use', '******') or die('error conecint');
echo "Connected to mysql~! <br />";
mysql_select_db('a3393387_c2db');
echo "Connected to database~! <br />";
echo "<b>Displaying details...</b> <br /><br />";
$query1 = "SELECT * FROM testing";
$result1 = mysql_query($query1) or die('cant select tables');
while($row = mysql_fetch_array($result1)) {
echo $row['fn'] ."-". $row['ln'] ."-". $row['ff'] ."-". $row['fh'] ."-". $row['yb'] ."-". $row['mar'];
echo "<br />";
}





mysqli_close($dbc);
?>

<a href="custom2.html">go back to steve's info form</a>

 

http://testo.comoj.com/custom2.php

 

Thanks!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.