Help - Search - Members - Calendar
Full Version: mySQL, xml and php
HTMLHelp Forums > Programming > Databases
Gooo
Hi

I have a few xml documents that are different events. In my mySQL database I'm storing the name (name in database) and a link (xml in database) to the corresponding xml document. i.e.
Event 1 --- event1.xml
Event 2 --- event2.xml
etc.

I'm using PHP to get the content from the database and display it in html. When I get the name of the event it displays correctly.

CODE
<?php
while($row = mysql_fetch_array($result))
{
    echo "<a href = 'main.php'> <div style = 'font-size:16pt'>" . $row['name'] . "</div> </a>";
}
setcookie("xmlDoc", $row['xml']);
?>


I'm trying to store $row['xml'] in a cookie so that I can access the xml's content in main.php.

What I want to do is when the user clicks on an event, that event's xml document must display in main.php. At the moment event1.xml's content is displaying when I click on Event 1 and Event 2.

What can I use (maybe onClick or something?) to store the xml document corresponding to the right $row['name'] in $row['xml']?


Thanks
Brian Chandler
Your question doesn't make sense. A cookie is info stored on the client computer -- it can't make something "accessible"(?) in main.php, which is a file on the server.

... hmm, I think I see... you are trying to "save" the row name by magic so main.php "knows" which it is. It doesn't work like that.

Just generate an html document with a list of links to the xml documents, which may or may not display appropriately. Why aren't you generating html documents, which will display appropriately?

Gooo
Thanks for your reply.

I'm now want to make a search page so that the user can search for a name in the database.
My code looks something like this:

CODE
<tr>
<td> Search: </td>
<td> <input type="text" name="search" size="20" maxlength="100" /> </td>
</tr>

********************

$searchQuery =  $_GET["search"];
$searchResult = mysql_query("SELECT * FROM content WHERE name LIKE '%$searchQuery%'") or die(mysql_error());

********************

<td> Result </td>
<td> <?php echo $searchResult; ?> </td>


This code isn't working yet because "Resource id #3" is displayed at the "Result".

What does this mean, and is my $searchResult correct?
Brian Chandler
QUOTE(Gooo @ Sep 28 2009, 11:24 PM) *

Thanks for your reply.

I'm now want to make a search page so that the user can search for a name in the database.
My code looks something like this:

CODE
<tr>
<td> Search: </td>
<td> <input type="text" name="search" size="20" maxlength="100" /> </td>
</tr>

********************

$searchQuery =  $_GET["search"];
$searchResult = mysql_query("SELECT * FROM content WHERE name LIKE '%$searchQuery%'") or die(mysql_error());

********************

<td> Result </td>
<td> <?php echo $searchResult; ?> </td>




What, all this on one page?? You need to send an html page including a form on which the user can select the search term (or whatever). Then in response you need to generate an html page with the results. You need to use the result from mysql_query(), which is a "handle" (or "Resource id #3" for example) with mysql_fetch_array().

See http://jp2.php.net/manual/en/function.mysql-fetch-array.php


You really need to read a mysql tutorial, or read the php manual for these mysql functions *carefully*. It is no good guessing that you just stick this here or that there, and the system will guess what you mean, because it won't.
Gooo
Thanks, the searching is working now.

When I use the following code, and a match in the database was found, the result is displayed. But when there isn't a match, the if part is never displayed. Is there maybe another way I can test for no match found?

CODE
<?php
while ($row = mysql_fetch_array($searchResult, MYSQL_ASSOC))
{
    echo $row["name"] . "<br>";  
}
if ($row != mysql_fetch_array($searchResult, MYSQL_ASSOC))
{
    echo "No match found";
}
?>


Brian Chandler
QUOTE(Gooo @ Sep 29 2009, 12:05 PM) *

Thanks, the searching is working now.

When I use the following code, and a match in the database was found, the result is displayed. But when there isn't a match, the if part is never displayed. Is there maybe another way I can test for no match found?

CODE
<?php
while ($row = mysql_fetch_array($searchResult, MYSQL_ASSOC))
{
    echo $row["name"] . "<br>";  
}
if ($row != mysql_fetch_array($searchResult, MYSQL_ASSOC))
{
    echo "No match found";
}
?>



Where do you get these bits of program you keep showing us? Have you written them yourself? Or copied something you don't really understand?

Do you understand what mysql_fetch_array() does? Have you read the manual, carefully?

IMPO, functions like mysql_fetch_array() are not a well-designed idea, because they have opaque semantics. The calls to mysql_fetch_array() with exactly the same arguments produce different results at different times... in particular a loop while (mysql_fetch_array()) goes on until there are no results left, then mysql_fetch_array() always returns FALSE. So what will happen if you ask if(mysql_fetch_array()) after it has "finished"?
Gooo
The code I'm posting is my own.

I figured it out. I'm declaring a variable at the beginning equal to 0.

When a result is found, I increment the variable.
Then I test if the variable is 0, if it is, no match was found.


Thanks for your help.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.