Use of undefined constant MYSQL_ASSOC

in 2019 I created a php page which created graphs via Google using data inserted into a DB:

<?php
include_once 'dbconfig.php';
$query = "SELECT * FROM elezioni2019_liste";
$result = mysql_query($query) or die(mysql_error());
while ($row= mysql_fetch_array($result, MYSQL_ASSOC)) {
	
$f1a =  $row["f1a"];
$f1b =  $row["f1b"];
and so on...

I need to update the page due to obsolete code and I did this:

include_once 'dbconfig.php';
$query = "SELECT * FROM elezioni2024_liste";
$result = mysqli_query($db_connection, $query) or die(mysql_error());
while($row= mysqli_fetch_array($result,MYSQL_ASSOC)) {
	
$f1a =  $row["f1a"];
$f1b =  $row["f1b"];
$f1_ele =  $row["f1_ele"];

but the function is wrong :slight_smile: Warning : Use of undefined constant MYSQL_ASSOC - assumed ‘MYSQL_ASSOC’ (this will throw an Error in a future version of PHP) in /var/www/vhosts/**/index.php on line 19

Warning : mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given in /var/www/vhosts/*/index.php on line 19

Can you help me please?
Thanks

Updating old mysql_ based code involves more than just getting the code to run without any errors. The mysql_ extension itself has been removed. What little protection magic_quotes provided for string values has been removed. Error handling has been switched to use exceptions.

As to the first error, you would need to use the corresponding mysqli defined constant or use the mysqli_fetch_assoc() function. As to the second error, the code inside your loop is probably assigning a string to the $result variable, causing the error after the loop has executed the first time. You also need to change the mysql_error() call to use mysqli_error(), which also requires the connection variable as a parameter.

To future proof your code, you should -

  1. Use the much simpler and better designed PDO extension.
  2. Set the character set to match your database tables (you should be doing this now, but it was almost never done in old code or worked correctly with the old database extensions.)
  3. Set the default fetch mode to assoc, so that you don’t need to specify it in each fetch statement.
  4. For the PDO extension, set emulated prepared queries to false, so that you use real prepared queries.
  5. Set the error mode to use exceptions (this is the default setting now in php8+.)
  6. Use a prepared query when supplying dynamic data to a query when it gets executed.
  7. Only catch and handle a database exception for user recoverable errors, such as when inserting/updating duplicate user submitted data.

For the query/code you have shown, after you make the connection using the PDO extension, it would look like -

$query = "SELECT * FROM elezioni2024_liste";
$stmt = $pdo->query($query);

while($row = $stmt->fetch())
{
	// your loop code using elements in $row
	
}

Typical PDO connection code -

$DB_HOST = ''; // database host name or ip address
$DB_USER = ''; // database username
$DB_PASS = ''; // database password
$DB_NAME = ''; // database name
$DB_ENCODING = 'utf8mb4'; // db character encoding. set to match your database table's character set. note: utf8 is an alias of utf8mb3/utf8mb4

$options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // set the error mode to exceptions. this is the default setting now in php8+
			PDO::ATTR_EMULATE_PREPARES => false, // run real prepared queries
			PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC // set default fetch mode to assoc
			];

$pdo = new pdo("mysql:host=$DB_HOST;dbname=$DB_NAME;charset=$DB_ENCODING",$DB_USER,$DB_PASS,$options);
2 Likes

Thank u very much for your answer

can u hel me again? i extract value in the row wher is present a fiscal code (cf, taken from a post)

$pdo = new pdo("mysql:host=$DB_HOST;dbname=$DB_NAME;charset=$DB_ENCODING",$DB_USER,$DB_PASS,$options);
	
	
$query = "SELECT elettori.cf, elettori.nome, elettori.n_seggio, elettori.indirizzo, elettori.iframe, elettori_fasce.fascia FROM elettori_fasce INNER JOIN elettori ON elettori_fasce.cf = elettori.cf where elettori.cf = '$cf'";
$stmt = $pdo->query($query);


while($row = $stmt->fetch()) {

if fc is presente i must print

echo nl2br("Codice Fiscale Inserito: $row[cf]\n");
				echo nl2br("Nome Elettore: $row[nome]\n");
				echo nl2br("Seggio Assegnato: Seggio N $row[n_seggio]\n");
				echo nl2br("Indirizzo: $row[indirizzo]\n");
..........

and work, but i want print else if cf is not presente.
i try wit if and else but not work


echo "fascia $row[fascia]\n";  //print fascia value
$ctrl = $row['fascia'];	//print fascia value
echo "control $ctrl\n"; //print ctrl value


if ($ctrl !== null) { 			
				echo nl2br("Codice Fiscale Inserito: $row[cf]\n");
				echo nl2br("Nome Elettore: $row[nome]\n");
				..........
				echo nl2br("<a href=index.php>Cerca un altro Codice Fiscale</a>\n");
		
				
	} else {
		echo nl2br("<b><center>Il codice fiscale inserito non risulta presente in archivio o non è stato digitato correttamente.</center></b>\n");
		echo nl2br("\n");
..........
		echo nl2br("<a href=index.php>inserisci il codice fiscale</a>\n");
		
	}						


tks

For this condition, is there a row in the elettori_fasce table or not? If there is a row, what value is in the fascia column?

Also, you should use a prepared query if the $cf value is from external data.

fascia value can be 1, 2 or 3. . every CF present in DB have a value, 1, 2 or 3.

in page index.php a user insert owner cf,

<form name="form1" method="post" action="cercaseggio.php">
	<td>
		<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
				<tr>
					<td style="text-align: center; "colspan="3"><strong>RICERCA SEGGIO ELETTORALE </strong></td>
				</tr>
				<tr>
					<td style="text-align: right"; width="250">Codice Fiscale :</td>
					<td width="350"><input name="cf" type="text" id="cf"></td>
				</tr>
				<tr>
					<td style="text-align: center; "colspan="3"><input type="submit" name="Submit" value="Cerca il seggio"></td>
				</tr>
			</table>
	</td>
</form>

and i gei it in this page (sorry for my english…)

$cf = $_POST["cf"];

if i try with a present cf, whith fascia=1 for exemple, te code

echo "fascia $row[fascia]\n";  //print fascia value
$ctrl = $row['fascia'];	//print fascia value
echo "control $ctrl\n"; //print ctrl value

print correctly fascia 1 control 1 and all the other, but if cf is not present, dont prit nothing after else

You seem to be asking how to display something if the query didn’t match any data?

To do this, you need to change the program logic so that it fetches all the data from the query into a php variable (see the fetchAll() method.) You can then test if there is or is not any data.

This is what I think you mean. This also uses a prepared query -

$query = "SELECT elettori.cf, elettori.nome, elettori.n_seggio, elettori.indirizzo, elettori.iframe,
 elettori_fasce.fascia
 FROM elettori_fasce
 INNER JOIN elettori ON elettori_fasce.cf = elettori.cf
 where elettori.cf = ?";

$stmt = $pdo->prepare($query);
$stmt->execute([ $_POST["cf"] ]);
$data = $stmt->fetchAll();


// at the point of producing the output
if(empty($data))
{
	echo nl2br("<b><center>Il codice fiscale inserito non risulta presente in archivio o non è stato digitato correttamente.</center></b>\n");
	echo nl2br("\n");
	..........
	echo nl2br("<a href=index.php>inserisci il codice fiscale</a>\n");
}
else
{
	// loop to produce the output
	foreach($data as $row)
	{
		echo nl2br("Codice Fiscale Inserito: $row[cf]\n");
		echo nl2br("Nome Elettore: $row[nome]\n");
		..........
		echo nl2br("<a href=index.php>Cerca un altro Codice Fiscale</a>\n");
	}
}

Also, for the User Interface (UI), you should use a select/option menu with the possible choices.

thank you so much. Thank u!

Sponsor our Newsletter | Privacy Policy | Terms of Service