Jump to content

Problem with mysqli_fetch_array


rocky48
Go to solution Solved by PFMaBiSmAd,

Recommended Posts

I HOPE THIS IS IN THE CORRECT FORUM!

 

I am developing a program that filters a MySQL database to choose a type of verse.

I am using an HTML form with check boxes to select the type.  The variable that is passed using $POST is a text string.

I am using this string to query the database in the table Events to get all of the verses with that Event_Type.

 

Using mysqli_fetch_array, I am then extacting the ID to use in another MySQL query, this time querying the Verses table to list the verses of this type.

 

What is actually happening is that all the types up to 9 are displaying the correct result, but the ones with ID 10 to 19 are displaying verses with ID = 1 and the last 3 are displaying versed with ID = 2.

 

So it is obvious that it is only looking at the first digit of the ID.

 

How do you get it to recognise complete ID number?

 

PHP Version 5.3.6

Here is the PHP code:

<?php
include("Loc_cverse_connect.php");
doDB();

//check for required info from the query string


//verify the Event exists
$verify_Event_sql = "SELECT ID, Event_Type FROM Events WHERE Event_Type = '".$_POST["Event_Type"]."'";
$verify_Event_res =  mysqli_query($mysqli, $verify_Event_sql) or die(mysqli_error($mysqli));

echo  $_POST["Event_Type"];

if (mysqli_num_rows($verify_Event_res) < 1) {
//this Event does not exist
$display_block = "<p><em>You have selected an invalid Event.<br/>
Please try again.</em></p>";
} else {
//get the Event ID
while ($Event_info = mysqli_fetch_array($verify_Event_res)) {
	$Event_ID = stripslashes($Event_info['ID']);
}


//gather the Events
$get_Event_sql = "SELECT ID, Verse FROM Verses WHERE Event = '".$Event_ID["ID"]."' ORDER BY ID ASC";
$get_Event_res = mysqli_query($mysqli, $get_Event_sql) or die(mysqli_error($mysqli));

//create the display string
$display_block = "
<p> The Event Type is <b>" .$_POST["Event_Type"]."</b> </p>
<table width=\"50%\" cellpadding=\"3\" cellspacing=\"1\" border=\"1\" BGCOLOR=\"#87CEEB\" >
<tr>
<th>ID</th>
<th>VERSE</th>
</tr>";

while ($Verse_info = mysqli_fetch_array($get_Event_res)) {
	$Event_id = $Verse_info['ID'];
	$Verse_text = nl2br(stripslashes($Verse_info['Verse']));

	//add to display
 	$display_block .= "
	<tr>
	<td width=\"1%\" valign=\"top\">".$Event_id."<br/></td>
	<td width=\"35%\" valign=\"top\">".$Verse_text."<br/><br/>
	</tr>";
}

//free results
mysqli_free_result($get_Event_res);
mysqli_free_result($verify_Event_res);

//close connection to MySQL
mysqli_close($mysqli);

//close up the table
$display_block .= "</table>";
}

?>
<html>
<head>

<title> List of Verses</title>
</head>
<body BGCOLOR="#87CEEB">
<h1>Verses</h1>
<?php echo $display_block; ?>
</body>
</html>

 

mysqlnd 5.0.8-dev - 20102224 - $Revision: 308673 $

MySQL Table Structure:

Event:-

ID

Event_Type

 

Verses:-

ID

Event

Sub_Type

Verse

 

Link to comment
Share on other sites

  • Solution

The variable - $Event_ID contains the id. Using $Event_ID["ID"] is attempting to treat the id number as an array and while it probably should not work, php is a loosely typed language and that is treating the id number as a string and is getting just the first character of the id.

 

Just use $Event_ID

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.