Reference: What Is a Perfect Code Sample Using the MySQL Extension

Reference: What is a perfect code sample using the MySQL extension?

My stab at it. Tried to keep it as simple as possible, while still maintaining some real-world conveniences.

Handles unicode and uses loose comparison for readability. Be nice ;-)

<?php

header('Content-type: text/html; charset=utf-8');
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
// display_errors can be changed to 0 in production mode to
// suppress PHP's error messages

/*
Can be used for testing
$_POST['id'] = 1;
$_POST['name'] = 'Markus';
*/

$config = array(
'host' => '127.0.0.1',
'user' => 'my_user',
'pass' => 'my_pass',
'db' => 'my_database'
);

# Connect and disable mysql error output
$connection = @mysql_connect($config['host'],
$config['user'], $config['pass']);

if (!$connection) {
trigger_error('Unable to connect to database: '
. mysql_error(), E_USER_ERROR);
}

if (!mysql_select_db($config['db'])) {
trigger_error('Unable to select db: ' . mysql_error(),
E_USER_ERROR);
}

if (!mysql_set_charset('utf8')) {
trigger_error('Unable to set charset for db connection: '
. mysql_error(), E_USER_ERROR);
}

$result = mysql_query(
'UPDATE tablename SET name = "'
. mysql_real_escape_string($_POST['name'])
. '" WHERE id = "'
. mysql_real_escape_string($_POST['id']) . '"'
);

if ($result) {
echo htmlentities($_POST['name'], ENT_COMPAT, 'utf-8')
. ' updated.';
} else {
trigger_error('Unable to update db: '
. mysql_error(), E_USER_ERROR);
}

What is the error in my code that I get mysql_fetch_array warning?

$query will be FALSE because the query you have entered is not valid; the return from mysql_query is FALSE when there was some error compiling or executing the query.

Change LIMT to LIMIT and try again.

Need assistance with a mysql error

mysql_query returns false if there is an error.

You should put a check like:

if(!$listings_query) {
die('An error occurred. The message is: ' . mysql_error());
}

Your SQL statement looks a bit funky, so you should also print out the actual query and make sure it looks fine.

Query PHP / MYSQL looking up Lname

$result = mysql_query("SELECT * FROM {$table} WHERE `Lname`='".mysql_real_escape_string($Lname)."'");

Php Mysql Select command issue

Try and debug your script to make sure you actually have values for your select statement... echo out your variables to make sure they have data... check it right before you do your select statement, if they are coming up empty make sure that you instantiate the db connection before running mysql_real_escape_string or it won't work...

php mysql_fetch_array

Update:

You are not doing anything inside your loop:

while($list = mysql_fetch_array($result));

Try:

while($list = mysql_fetch_array($result){
echo $list['mandant_kurz'];
}

Also try running your query in MySQL client to make sure it actually returns 100 rows.


You will have to use loop:

while($row = mysql_fetch_array($result)){
echo $row['mandant_kurz'];
}

Running a query while within a mysql_fetch_object while loop causing error

2 things: you haven't declared $db as a global variable within your function (so it's undeclared, causing the error you're seeing). Add the following:

global $db;

Second, remove the function keyword from inside your while loop. That's likely to cause php to biff.



Related Topics



Leave a reply



Submit