473,387 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

mysql_fetch_array() problem

201 100+
Good evening -

thanks in advance for you help!

attached is my query, and html table layout.

I'm trying to query 3 tbles in one select statement, and return the data to html table.

it work before when i had it broken down into three select statements and three results.
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query("select a.user_Id, AES_DECRYPT(`a.UEA`, '$userpass') as UEA, `a.UEA` as UEA1, AES_DECRYPT(`a.UEAP`, '$userpass') as UEAP, `a.UEAP` as UEAP1, b.user_Id_fk, b.Fname, c.user_Id_fk, c.Zip from user a, uname b, uzip c");
  2.  
  3. // create table layout
  4.  
  5. echo "<table border='1'>
  6. <tr>
  7. <th>UserId</th>
  8. <th>UserName</th>
  9. <th>EncUserName</th>
  10. <th>UserPassword</th>
  11. <th>EncUserPassword</th>
  12. <th>UserId</th>
  13. <th>FirstName</th>
  14. <th>UserId</th>
  15. <th>Zip</th>
  16. </tr>";
  17.  
  18. while ($row = mysql_fetch_array($result))
  19. {   
  20.     echo "<tr>";
  21.     echo "<td>" . $row ['a.user_Id'] . "</td> "; 
  22.     echo "<td>" . $row ['UEA'] . "</td>";
  23.     echo "<td>" . $row ['UEA1'] . "</td>";
  24.     echo "<td>" . $row['UEAP'] . "</td>"; 
  25.     echo "<td>" . $row['UEAP1'] . "</td>"; 
  26.     echo "<td>" . $row ['b.user_Id_fk'] . "</td>"; 
  27.     echo "<td>" . $row ['b.Fname'] . "</td>";
  28.     echo "<td>" . $row ['c.user_Id_fk'] . "</td>";
  29.     echo "<td>" . $row ['c.Zip'] . "</td>";
  30.     echo "</tr>";
  31.     //echo "<br />";
  32. }
  33.     echo "</table>";  
  34.  
  35. mysql_close($con);
Feb 5 '09 #1
13 2582
Atli
5,058 Expert 4TB
Hi.

There is nothing essentially wrong with the query. It looks perfectly valid.

Your join syntax may be off tho. I can't really tell without knowing your table structures.

I suggest you read through 12.2.8.1. JOIN syntax in the reference manual.
It explains the various types of joins you can use. Perhaps you need to be using a different type.
Feb 6 '09 #2
wizardry
201 100+
ok thanks will look into the tbl joins.

here is the table structure.

Expand|Select|Wrap|Line Numbers
  1. SET FOREIGN_KEY_CHECKS=0;
  2.  
  3. DROP DATABASE IF EXISTS `userAccount`;
  4.  
  5. CREATE DATABASE `userAccount`
  6. CHARACTER SET `utf8`
  7. COLLATE `utf8_unicode_ci`;
  8.  
  9. USE `userAccount`;
  10.  
  11. DROP TABLE IF EXISTS `user`;
  12.  
  13. CREATE TABLE `user` (
  14. `user_Id` bigint UNSIGNED AUTO_INCREMENT COMMENT 'user schema primary key',
  15. `UEA` longblob COMMENT 'user account name email address',
  16. `UEAP` longblob COMMENT 'user password',
  17. PRIMARY KEY (`user_Id`)
  18. ) ENGINE = InnoDB;
  19.  
  20.  
  21. DROP TABLE IF EXISTS `uname`;
  22.  
  23. CREATE TABLE `uname` (
  24. `uname_Id` bigint UNSIGNED not null COMMENT 'user infomation schema primary key',
  25. `user_Id_fk` bigint UNSIGNED auto_increment COMMENT 'users admin master primary key',
  26. `Fname` varchar(50) COMMENT 'user first name',
  27. `Lname` varchar(50) COMMENT 'user last name',
  28. PRIMARY KEY (`user_Id_fk`, `uname_Id`),
  29. constraint user_Id_fk_uname foreign key (`user_Id_fk`)
  30. references `user`(`user_Id`)
  31. on update cascade
  32. on delete cascade
  33. ) ENGINE = InnoDB;
  34.  
  35. DROP TABLE IF EXISTS `uzip`;
  36.  
  37. CREATE TABLE `uzip` (
  38. `user_Id_fk` bigint UNSIGNED auto_increment COMMENT 'user schema foreign key',
  39. `uzip_Id` bigint UNSIGNED COMMENT 'user zip primary key',
  40. `Zip` varchar(17) COMMENT 'user zip',
  41. PRIMARY KEY (`user_Id_fk`),
  42. constraint user_Id_fk_uzip foreign key(`user_Id_fk`)
  43. references `user`(`user_Id`)
  44. on update cascade
  45. on delete cascade
  46. ) ENGINE = InnoDB;
  47.  
thanks in advance for you help.
Feb 6 '09 #3
wizardry
201 100+
this is the error that i get when i placed the echo . error

codeError: Duplicate entry '22' for key 'PRIMARY'

i run a query in sqlplus and it returns 22 records.

this worked fine when i used a single select for each table. now that i joined them and want to layout the data in html table it errors.
Feb 6 '09 #4
Atli
5,058 Expert 4TB
Hmm... that's a weird error to get for a SELECT query.
That is something I would expect to get from a INSERT or a UPDATE query, not a SELECT query. Are you sure this is correct?

In any case, two things came to my attention when I looked closer at your code.

When you do:
Expand|Select|Wrap|Line Numbers
  1. SELECT a.First, b.Second, c.Third FROM a, b, c
The results will not include the table name you used to identify the columns.
So to use the results of this query in PHP
Expand|Select|Wrap|Line Numbers
  1. // This is incorrect:
  2. echo $resultRow['a.First'];
  3.  
  4. // It should be:
  5. echo $resultRow['First'];
And, because of this, it is not a good idea to have two columns share a name. Two elements can't share a name in a PHP array, so one of them would never make it to your PHP result array.

So, you should create aliases for the 'user_Id_fk' fields in your query. That way you can be sure you are reading them correctly in your PHP code.
Feb 6 '09 #5
wizardry
201 100+
thanks you, i will look into it. I ran the query fine in sqlplus on the database with the exception not being able to decrypt.

i will rename the columns i have tags for in php. Thanks i just assumed it was reading the array, and the objects in the array not just the field name.

i will repost once i've tested.
Feb 6 '09 #6
wizardry
201 100+
alright i made the changes as suggested but still this Error: Duplicate entry '1' for key 'PRIMARY'

I'm mainly oracle/sql server dba not apps

thanks again for your help in advance.

here is my code in the page that is erroring out.

i've copied the last inserted id to the 2 child tables to accomidate the matching puzzle piece. also i've tried throwing nulls at the auto_inc but that just errored out.


[config.php]
$sql=("insert into user ( user_Id, UEA, UEAP) values (null, AES_ENCRYPT('$_POST[username]', '$userpass'), AES_ENCRYPT('$_POST[password]', '$userpass'))");

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}

echo " 1 Record added in users account schema";
//uname table
$sql=("insert into uname ( user_Id_fk, uname_Id, Fname, Lname) values ( LAST_INSERT_ID(), LAST_INSERT_ID(), '$_POST[firstname]', '$_POST[lastname]')");

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo " 1 Record added users name schema";
// zip code table
$sql=("insert into uzip ( user_Id_fk, uzip_Id, Zip ) values (LAST_INSERT_ID(), LAST_INSERT_ID(), '$_POST[zip]')");

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}

echo " 1 Record added to users schema zip code";
//*
$result = mysql_query("select a.user_Id as UserId,
a.UEA,
a.UEAP,
b.user_Id_fk as NameId, b.Fname,
c.user_Id_fk as ZipId, c.Zip
from
user a,
uname b,
uzip c
where
UserId=NameId and ZipId=UserId)");

if (!mysql_query($sql,$con))
{
die(' Error: ' . mysql_error());
}


// create table layout

echo "<table border='1'>
<tr>
<th>UserId</th>
<th>UserName</th>
<th>UserPassword</th>
<th>NameId</th>
<th>FirstName</th>
<th>ZipId</th>
<th>Zip</th>
</tr>";

while ($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row ['UserId'] . "</td> ";
echo "<td>" . $row ['UEA'] . "</td>";
echo "<td>" . $row['UEAP'] . "</td>";
echo "<td>" . $row ['NameId'] . "</td>";
echo "<td>" . $row ['Fname'] . "</td>";
echo "<td>" . $row ['ZipId'] . "</td>";
echo "<td>" . $row ['Zip'] . "</td>";
echo "</tr>";

}
echo "</table>";
//*/
mysql_close($con);
?>
[/config.php]
Feb 6 '09 #7
r035198x
13,262 8TB
@wizardry
After copying the las tinsert id to the first child table, the id of that child table becomes the new last insert id. Are you sure that is the id that you want to copy to the second child table?
Feb 6 '09 #8
wizardry
201 100+
yes thats what i want to add because it grabs the parent id and inserts in into the child records per that client connection. if another connection was to run it would have its own process thread.

everything worked fine until i changed the way i wanted to layout the return form.

i had 3 seperate results and spread the data out into html tbl i like the way this table will look.

but any how

it does insert the data it just errors and doesn't return the data.
Feb 6 '09 #9
wizardry
201 100+
ok i took out a error handler and after the $results query and this is the actual error. mysql_fetch_array() supplied argument is not a valid MySQL

so it still relates back to the query.

i dont understand this the query im running now runs fine on the sqlplus but not in php?

thanks in advance for your help
Feb 6 '09 #10
r035198x
13,262 8TB
Echo out the sql that is being executed and post it here. When you said the last_insert_id() are inserting fine, do you mean that you have say a parent table P, a child table A (child of P) and another child table B which is a child of A rather than a child of P? Because that is what your inserts will do.
Feb 6 '09 #11
wizardry
201 100+
ok, here is the schema!

parent a 1=> 1 child b and child c 1 => 1 to a

A
/----\
B-----C

these are a required data tables i could use use c 1=> 1 to b
Feb 6 '09 #12
wizardry
201 100+
it was the results query i needed to change a couple single quotes to back slash.

here is the completed results.

[php]
$sql=("insert into user ( user_Id, UEA, UEAP) values (null, AES_ENCRYPT('$_POST[username]', '$userpass'), AES_ENCRYPT('$_POST[password]', '$userpass'))");

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}

echo " 1 Record added in users account schema";
//uname table
$sql=("insert into uname ( user_Id_fk, uname_Id, Fname, Lname) values ( LAST_INSERT_ID(), LAST_INSERT_ID(), '$_POST[firstname]', '$_POST[lastname]')");

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo " 1 Record added users name schema";
// zip code table
$sql=("insert into uzip ( user_Id_fk, uzip_Id, Zip ) values (LAST_INSERT_ID(), LAST_INSERT_ID(), '$_POST[zip]')");

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}

echo " 1 Record added to users schema zip code";
//*
$result = mysql_query("select a.user_Id as UserId,
a.UEA,
AES_DECRYPT(a.UEA, '$userpass') as UEAD,
a.UEAP,
AES_DECRYPT(a.UEAP, '$userpass') as UEAPD,
b.user_Id_fk as NameId,
b.Fname,
c.user_Id_fk as ZipId,
c.Zip
from
user a,
uname b,
uzip c
where a.user_Id=b.user_Id_fk and c.user_Id_fk=a.user_Id");

// create table layout
//where 'a.user_Id'='b.uname_Id_fk' and 'c.uzip_Id'='a.user_Id'
echo "<table border='1'>
<tr>
<th>UserId</th>
<th>UserName</th>
<th>EncUserName</th>
<th>UserPassword</th>
<th>EncUserPassword</th>
<th>NameId</th>
<th>FirstName</th>
<th>ZipId</th>
<th>Zip</th>
</tr>";

while ($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row ['UserId'] . "</td> ";
echo "<td>" . $row ['UEA'] . "</td>";
echo "<td>" . $row ['UEAD'] . "</td>";
echo "<td>" . $row ['UEAP'] . "</td>";
echo "<td>" . $row ['UEAPD'] . "</td>";
echo "<td>" . $row ['NameId'] . "</td>";
echo "<td>" . $row ['Fname'] . "</td>";
echo "<td>" . $row ['ZipId'] . "</td>";
echo "<td>" . $row ['Zip'] . "</td>";
echo "</tr>";

}
echo "</table>";
//*/
mysql_close($con);
?>
[/php]

thanks, again for your help!
Feb 6 '09 #13
wizardry
201 100+
hello again - some what a same problem. didn't want to start a new thread.

i'm using dw for the designer. and i'm tring to recreate what i did in the last post; using their auto forms wizard.

i've changed the connections.php to reflect what database its going to.

here is the error: Table 'uname.user' doesn't exist

I thought another set of eyes could help! thanks in advance for you help!

here is the code:
[php]
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO user (user_Id, UEA, UEAP) VALUES (null, %s, %s)",
GetSQLValueString($_POST['user_Id'], "int"),
GetSQLValueString($_POST['UEA'], "text"),
GetSQLValueString($_POST['UEAP'], "text"));

$insertSQL1 = sprintf("insert into uname (uname_Id, user_Id_fk, Fname, Lname) values ( LAST_INSERT_ID(), LAST_INSERT_ID(), %s, %s )", GetSQLValueString($_POST['uname_Id'], "int"),
GetSQLValueString($_POST['user_Id_fk'], "int"),
GetSQLValueString($_POST['Fname'], "text"),
GetSQLValueString($_POST['Lname'], "text"));

$insertSQL2 = sprintf("insert into uzip (uname_Id_fk, uzip_Id, Zip) VALUES (LAST_INSERT_ID(), null, %s,)", GetSQLValueString($_POST['uname_Id_fk'], "int"),
GetSQLValueString($_POST['uzip_Id'], "int"),
GetSQLValueString($_POST['Zip'], "text"));

//----------------- db name ------------- db connection
mysql_select_db($database_user, $Schema);
$Result1 = mysql_query($insertSQL, $Schema) or die(mysql_error());

mysql_select_db($database_uname, $Schema);
$Result2 = mysql_query($insertSQL1, $Schema) or die(mysql_error());

mysql_select_db($database_uzip, $Schema);
$Result3 = mysql_query($insertSQL2, $Schema) or die(mysql_error());

$insertGoTo = "userdataset.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<table align="center">
<tr valign="baseline">
<td nowrap="nowrap" align="right">Email Address:</td>
<td><input type="text" name="UEA" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Password:</td>
<td><input type="password" name="UEAP" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">First Name:</td>
<td><input type="text" name="Fname" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Last Name:</td>
<td><input type="text" name="Lname" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Zip:</td>
<td><input type="text" name="Zip" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">&nbsp;</td>
<td><input type="submit" value="Insert record" /></td>
</tr>
</table>
<input type="hidden" name="user_Id" value="" />
<input type="hidden" name="uname_Id" value="" />
<input type="hidden" name="user_Id_fk" value="" />
<input type="hidden" name="uname_Id_fk" value="" />
<input type="hidden" name="uzip_Id" value="" />
<input type="hidden" name="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
</body>
</html>
[/php]
Feb 7 '09 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Robert | last post by:
<?php echo "HI " . $name; $result = mysql_query("SELECT * FROM album WHERE username='{$name}'"); while ($next_row = mysql_fetch_array($result)) { $album_name = $next_row; echo "<a...
15
by: Matthew Robinson | last post by:
I set up phpmyadmin, and it works fine, but my code isn't working. id is the correct column (it is the primary key), stock is the correct database. CODE: $qresult = mysql_query("SELECT id FROM...
5
by: james | last post by:
I am new to PHP and am trying to run a simple query and display the result, with no luck. Here is the code I am using. <?php //start session session_start(); //store cmpid from querystring...
4
by: Konrad | last post by:
In the part of code: $polecenie = "SELECT osoby.Id ,osoby.Imie , osoby.Nazwisko,osoby.Tytul,osoby.Email,adrespraca.Adres AS ap, adrespraca.KodPoczt AS...
15
by: Good Man | last post by:
Hey there I have a dumb question.... Let's say i have a database full of 4000 people.... I select everything from the database by: $result = mysql_query("SELECT * FROM People");
3
by: Pratchaya | last post by:
Hi Everyone ============================================================== About PHP::: Error/Problem PHP Warning: mysql_fetch_array():...
4
by: Marcel Brekelmans | last post by:
Hello, I seem to get an extra empty field in every 'mysql_fetch_array' command I issue. For example: I have a simple table 'tblName': ID Name 1 Jane 2 Joe 2 Doe
9
by: Petr Vileta | last post by:
Hi, I'm new here and excuse me if this question was be here earlier. I have a simple code <html><body> <?php <?php $link = mysql_connect("localhost", "user", "password") or die("Grr: " ....
1
by: student2008 | last post by:
Sorry about the title its a tricky one. I have a form which allows me to add a question and answers into a mysql database via a combination of, if a certain option is chosen and the reset button...
11
by: chemlight | last post by:
I'm having a problem. I'm sure I'm going to kick myself over the answer... I have a table that stores vendors and their languages. This table starts out blank. I am querying the table to see if a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.