This script works with require_once and produces warnings for line 6 when I try to use $mysqli = new... (which I've commented out). Please help me with this script:

[codebox]<?php

require_once "connect_to_mysql.php";

//$mysqli = new mysqli("localhost","user","pw","db");

$result = mysql_query("SELECT * FROM example")

or die(mysql_error());

echo "<table border='1'>";

echo "<tr> <th>Name</th> <th>Age</th> </tr>";

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

echo "<tr><td>";

echo $row;

echo "</td><td>";

echo $row;

echo "</td></tr>";

}

echo "</table>";

//mysqli_close($mysqli);

?> [/codebox]

Thank-you

Recommended Answers

You can use the following code,

<?php
  $cn = new mysqli("localhost","user","pw","db");
  if(!$cn) die("Fails");
  $result=$cn->query("select * from TableName");
  if(!result) die("Fails");
  while($row=$result->fetch_row()){
    // put your statements
  }
  $result->free();
  $cn->close();
?>

Please refere this link -

Jump to Post

All 3 Replies

You can use the following code,

<?php
  $cn = new mysqli("localhost","user","pw","db");
  if(!$cn) die("Fails");
  $result=$cn->query("select * from TableName");
  if(!result) die("Fails");
  while($row=$result->fetch_row()){
    // put your statements
  }
  $result->free();
  $cn->close();
?>

Please refere this link - http://php.net/manual/en/book.mysqli.php

Aliases and deprecated Mysqli Functions

For require_once, I thought the syntax was like this:

require_once('connect_to_mysql');

And also, couldn't you use this function to connect to MySQL?

$cn = mysqli_connect("localhost", "user", "pw", "db");

So to connect and request info from the database you could do something like this

$cn = mysqli_connect("localhost", "user", "pw", "db");

//set the query in a variable
$query = "SELECT * FROM table";

$result = mysqli_query($cn, $query);

if(!$result) die("Query fail");

Then you could just continue with your while loop, since there isn't an error with that, right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.