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

Dani AI

Generated

Main issues in the posted script: it mixes the old mysql_* API with mysqli (they are not compatible), it treats a fetched row as a string (echoing $row prints "Array"), and connection/error checks are either missing or done incorrectly. Also confirm what connect_to_mysql.php actually does — if that file already opens a mysql or mysqli connection, creating another one will conflict or be unnecessary.

Good points already posted by and — both show valid directions (OO-style mysqli and procedural mysqli). A couple of corrections to their notes: after new mysqli(...) check $conn->connect_errno / $conn->connect_error (new mysqli always returns an object, so if(!$conn) is not a reliable check), and any typo like if(!result) needs the dollar sign (if(!$result)). Procedural mysqli_connect() is fine if used consistently.

Practical checklist to fix the thread code:

  • Inspect connect_to_mysql.php to see whether it creates a mysql_* or mysqli connection and whether it defines a connection variable (use that instead of recreating one).
  • Pick one API (mysqli or PDO) and use it consistently; do not mix mysql* and mysqli* calls.
  • When fetching rows use field names or indexes (for example $row['name'] or $row[0]), not echo $row.
  • Turn on mysqli error reporting while developing (mysqli_report) or check $mysqli->error / $mysqli->connect_error for diagnostics.
  • Escape HTML output (e.g., htmlspecialchars) and use prepared statements for any user input.

Example (mysqli, object-oriented, safe output):

<?php
// make sure connect_to_mysql.php does not already create/close the same $mysqli
require_once 'connect_to_mysql.php';

$mysqli = new mysqli('localhost','user','pw','db');
if ($mysqli->connect_errno) die('Connect error: '.$mysqli->connect_error);

$sql = "SELECT name, age FROM example";
if ($res = $mysqli->query($sql)) {
  echo "<table border='1'><tr><th>Name</th><th>Age</th></tr>";
  while ($row = $res->fetch_assoc()) {
    echo '<tr><td>'.htmlspecialchars($row['name']).'</td><td>'.(int)$row['age'].'</td></tr>';
  }
  echo '</table>';
  $res->free();
}
$mysqli->close();

These changes address the core problems seen in 's code and build on the suggestions from and .

Recommended Answers

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.