this part of php that connected with db

$q2="select *from emp order by id";
$r2=mysql_query($q2)or dai("query faild:"mysql_error());

while($x=mysql_fetch_array($r2)
 {

if($x["depno"]==10)//dose it right to use $x=["depno"]like this when i use the query select* from the table??
{
echo"<tr><td>$x["emloyeeno"]</td>";
echo"<td>$x["employee name"]</td>";
echo"<tr><td>$x["address"]</td>";
echo"<td>$x["salary"]</td>";
$sum=$sum+$x["salary"];
}
}

Dani AI

Generated

Short, practical clarification for the issues raised by and the replies from , and .

Your immediate problems are (1) array key quoting and string-quoting inside echo, (2) a misspelled error function (dai() vs die()), and (3) the fact that the old mysql_* API is no longer recommended. The original ext/mysql functions were deprecated and later removed — new code should use MySQLi or PDO. (php.net)

Always quote string keys and avoid the legacy bareword form like $row[key] — that relies on PHP treating an undefined constant as a string and will produce notices or become an error in newer PHP versions. Use quoted keys and one of these safe patterns:

// interpolation with curly syntax (works inside double quotes)
echo "<tr><td>{$row['id']}</td><td>{$row['name']}</td></tr>";

// or concatenate and escape (clear and robust)
echo '<tr><td>' . htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8') . '</td>'
   . '<td>' . htmlspecialchars($row['name'], ENT_QUOTES, 'UTF-8') . '</td></tr>';

For string interpolation of array elements the “complex (curly) syntax” avoids parsing collisions and is the recommended approach when embedding values into double-quoted strings. (php.net)

Don’t rely on mysql_query() / mysql_fetch_* in new code — use prepared statements (MySQLi or PDO) to avoid SQL injection and to work on current PHP versions. Example (PDO or MySQLi) patterns let you bind depno and iterate safely while summing salary. For prepared-statement usage see the mysqli/PDO docs. (php.net)

Finally, die() (alias of exit) is the correct builtin name; using it for quick debugging is fine, but prefer proper exception handling or logging in production. (php.net)

Notes tied to replies: — fetching as an object is valid, but prefer PDO with PDO::FETCH_ASSOC or PDO::FETCH_OBJ consistently. — unquoted keys “work” by accident but are fragile. — it’s die(), not dai().

Recommended Answers

All 3 Replies

im not sure really if i have the right to answer ur question
or it is just the business of experts here

anyway.. i don't have time to try it it works or not
try it and tell us if it assigns an error here

for me i will do it like this and it works..

//just an example..
$row = mysql_fetch_object($result);

//use it like this..
$row->age

i hope i could help
good luck

u dont need to have the "'s in there just $x[thething]

or dai()

did you mean die() or is it your func!

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.