PHP - Mysql_fetch_array Killing Me A Little..
I have some OOP code im working on and I have some interactions with the database. However, mysql_fetch_array fails to be working. Have I missed something?
Code: [Select] class core { var $array_batsmen = ''; var $array_bowlers = ''; // Query to get batsmen function get_batsmen() { return $this->array_batsmen = mysql_query('SELECT id, name, ability, strength FROM wtw_players WHERE team_id = 1 ORDER BY id ASC'); } // Query to get bowlers function get_bowlers() { $this->array_bowlers = mysql_query('SELECT id, name, ability, strength FROM wtw_players WHERE team_id = 2'); } // Generate array of batsmen to be used function generateBatsmen() { while ($array = mysql_fetch_array($this->array_batsmen)) { echo $array['name']; } } } The functions are called later normally, i've only included the necessary parts. Basically I have the var $array_batsmen which stores the mysql_query. This is then later used with a mysql_fetch_array to return names from the earlier query. But nothing happens! It doesn't return anything (don't worry, I have a return later on). The loop just doesn't seem to work :/ Similar TutorialsHey there everyone. Today isn't my lucky day since I'm dealing with international characters. (Arabic). What happens is the following: Arabic entered in form > saved to MySQL > Retrieved from MySQL > outputted as XML > used in an application (Google maps if it makes any difference). Plain and simple, the problem is that the final output is this: Code: [Select] %u0645%u0639%u0644%u0645 which definetly isn't arabic. What I've tried so far: - Loading the Google Maps thing directly from XML which I manually created and put the arabic chars into -> WORKED! (meaning I cut out the saving and retrieval of info from the db to see where the problem may be coming from) - Set the database collation and charset to utf8_unicode_ci. (Also collation of the fields inside the table) -> NO LUCK! - Tried encoding all PHP files and html files involved in the process to UTF-8 Code: [Select] <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">-> NO LUCK! - Tried a suggestion from Google which said try to execute this query after connection with the db: mysql_query("SET NAMES 'utf8'"); mysql_query('SET CHARACTER SET utf8'); -> still no luck First time I get that frustrated. I guess the problem isn't with Gmaps and XML. Something to do with PHP & MySQL because when the raw XML files displays correctly, Gmaps works like a charm. And the only way I got the XML file to show correctly as said earlier was to manually create it and enter the arabic text in it. Thanks a lot for any help provided! why is it that i remove the / from the var slide and but it cuts of the sentence where the ' was at for example i wrote : It's very sunny out! and it just showed It what mistake did i do wrong Code: [Select] <?php if($_POST['submitbtn']){ require "scripts/connect.php"; $slide = mysql_real_escape_string($_POST ['slide']); $news = mysql_real_escape_string($_POST ['news']); if($news){ $query = mysql_query("UPDATE home SET `slide`='$slide',`news`='$news'"); echo "You have add successfully"; }else $msg = "News section must be filled!"; mysql_close(); } ?> <?php require "scripts/connect.php"; $query = mysql_query("SELECT * FROM home"); $rows = mysql_fetch_assoc($query); $slide = stripslashes($rows['slide']); $news = stripslashes($rows['news']); mysql_close(); ?> <form action='edithp' method='post'> <table> <tr> <td></td> <td><?php echo $msg;?></td> </tr> <tr> <td>Slider</td> <td><input type="text" name='slide' style="width:380px" value=<?php echo $slide;?> /></td> </tr> <tr> <td>News</td> <td><textarea cols='45' rows='20' name='news'><?php echo $news;?></textarea></td> </tr> <tr> <td></td> <td><input type='submit' name='submitbtn' value='SUBMIT'</td> </tr> </table> </form> Ok, what I want to happen is, if if($_POST['select'] == 'choose'){ is submitted, the script to display an error and not proccess anything after with the die(); message displayed to the user, but it's not showing the message, and infact when if($_POST['select'] is submitted as another array (I'm sorry, is that the correct term?) it still kills the whole script. I've check in my db an nothing is updating the tables so I guess die is working to an degree. I've tried it without the die commands an it works as it should so I know the info does get sent without the die Here is my code so far - <?php /************************** database info ****************************/ $host = "***"; $user = "***"; $pass = "***"; $db = "***"; /**************************** define variables ****************************/ $name = $_POST['name']; $message = $_POST['message']; mysql_connect($host, $user, $pass) OR die ("Could not connect to the server."); mysql_select_db($db) OR die("Could not connect to the database."); // connect to server or die :( ?> <?php if(isset($_POST['submit'])) { if($_POST['select'] == 'choose'){ die("please select a shout or request from the drop down");// if no drop down is selected, kill the script and explain why } else if($_POST['select'] == 'shouts'){ // begin shouts $message = htmlspecialchars($message); $message = nl2br($message); $message = mysql_real_escape_string($message); $name = htmlspecialchars($name); $name = mysql_real_escape_string($name); $class = "shout"; // assigns the css class to be used when the results are called { mysql_query("INSERT INTO shoutbox (name, message, class) VALUES ('$name', '$message', $class)"); }} // begin request ?> <link href="css/vip_sheet.css" rel="stylesheet" type="text/css" /> <div id="shout-container"> <div id="shouts"> <?php $result = mysql_query("SELECT * FROM shoutbox ORDER BY id DESC LIMIT 0,10"); while ($row = mysql_fetch_array($result)) { ?> <div class="<?php echo $row['class']; ?>"> <b><?php echo $row['name']; ?></b><br /> <?php echo $row['message']; ?></div> <?php }} ?> </div> </div> thank you Hi I have 2 problems 1) I am making a script which will show NS records i.e. NS1 -- blah blah NS2 -- blah blah This is what I have done Code: [Select] $domain_name = $_POST[domainbox]; // From text Box $dns = dns_get_record($domain_name, DNS_NS); $ns_1 = $dns['0']; echo $ns_1['taget']; // This means NS1 = blah blah I did this because when we do echo of Code: [Select] $dns = dns_get_record($domain_name, DNS_NS), out put is Out put is Code: [Select] Array ( [0] => Array ( [host] => yourdomain.com [type] => NS [target] => ns1.yourdomain.com [class] => IN [ttl] => 70144 ) [1] => Array ( [host] => yourdomain.com [type] => NS [target] => ns2.yourdomain.com [class] => IN [ttl] => 70144 ) ) But it is not showing any output. My brain is screwed up, didnt found anything on google 2) If a domain have 4 or 8 i.e. more than 2 name servers, then how to do its output ? Hello Everyone, I am very very new to php, i recetly code some php script for my website, but now stuck with some horrible errors. At first the same scripts were running quite fine, but just today its start giving me erroe, i can't figure out whats the issue, i try to uninstall and install XAMPP again and again, configure php.ini file but still no result. following are the error message i am getting: Warning: include_once(C:/xampp/htdocs\include\search.php?start=0&page=0&product=black&cat=All Categories) [function.include-once]: failed to open stream: No error in C:\xampp\htdocs\initialsearch.php on line 6 Warning: include_once() [function.include]: Failed opening 'C:/xampp/htdocs\include\search.php?start=0&page=0&product=black&cat=All Categories' for inclusion (include_path='.;\xampp\php\PEAR') in C:\xampp\htdocs\initialsearch.php on line 6 i also set the include path in php.ini , but its also not helped. It will be a great help if someone can help me to solve this issue. i am attaching following files alone : initialsearch.php search.php Also , i am running WINDOWS 7 and XAMPP 1.7.3 Waiting for some help thanks again. select_display.php // I am selecting an ID value from the db, result can only be 0 or 1 if (mysql_num_rows($result) == 0) { print '<big style="color: red;"><span style="font-weight: bold;"> ERROR MESSAGE:</span></big> Update ID number not found'; // I don't want my form to be displayed, just the error message } if (mysql_num_rows($result) == 1) { // Here I want to jump to html <form action="insert.php" method="post"> // I don't want to echo the whole form from within php // Now it displays on either compare because I am displaying it after ?> } Hello there
After a few years of spending less and less time coding, I've got a lot of catching up to do. Back when I left I usually would run without classes. Now this is a big deal for me today.
I do understand the concept of classes and already did some working models, mostly from my learning process.
Now here is what is bothering me:
<?PHP class database { // Variables public $test; // Constructor public function __construct() { $test = "4"; } // Functions // public function test() { var_dump($this->test); } } $test = new database; $test->test(); ?>Wether I run this script on itself, nor through another file, this does work. What i get is: NULL The constructor does run, I did an echo inside it. Also it does not matter if the variable is public, private or protected - it will be always NULL. Error_reporting is on E_ALL, does not show any errors. What have I overlooked? Hi. I just have a quick question. If I perform a mysql query like the one below that returns all of the rows in a table. How do I then add all of this data into an array. If I use the following code bellow the variable $array will only contain the first row of the table. $rs = mysql_query($query, $conn); $array = mysql_fetch_array ($rs); Here is my code Code: [Select] // Show the events for this day: $getEvent_sql = "SELECT cal_title, cal_description, date_format(cal_start_time, '%l:%i %p') as fmt_date FROM calendar WHERE month(cal_start_time) = '".$m."' AND dayofmonth(cal_start_time) = '".$d."' AND year(cal_start_time)= '".$y."' ORDER BY cal_start_time"; $getEvent_res = myload($getEvent_sql); echo "<pre>"; print_r($getEvent_res); echo "</pre>"; if (count($getEvent_res) > 0){ $event_txt = "<ul>"; while($ev = mysql_fetch_array($getEvent_res)){ echo "Inside"; echo $ev; $event_title = stripslashes($ev["cal_title"]); $event_shortdesc = stripslashes($ev["cal_description"]); $fmt_date = $ev["fmt_date"]; $event_txt .= "<li><strong>".$fmt_date."</strong>: ".$event_title."<br/>".$event_shortdesc."</li>"; echo $event_title; } $event_txt .="</ul>"; mysql_free_result($getEvent_res); } else { $event_txt = ""; } if ($event_txt != ""){ echo "<p><strong>Today's Events:</strong></p> $event_txt <hr/>"; } I can't get inside the while loop. The array is populated, I've checked that. The count of $getEvent_res = 3. echo "Inside", and echo $ev give me nothing. Any ideas what I'm doing wrong? $getEvent_res = myload($getEvent_sql); The myload is a custom function that just works, I didn't write it, but I know it works. Hi everyone, Bit of a random question and im sure there is good reason so if someone could in lighten me that would be great ! With regards to the below code, it seems to just create an infinite loop when you put mysql_fetch_array. Just want to know what wrong with doing this? and why it act in this way ? Code: [Select] $sql = mysql_query("SELECT * FROM cities"); $results = mysql_fetch_array($sql); while($row = $results) { echo $row['City']."<br />"; } thanks Guys This is a pain. It seems totally logical to me and I had it working before I accidentally saved over the working version (major boo boo). This is two code snippets, first one is how I want it to work - One Query and the loop through results. Second does work, but then its going to have to run mysql 60 times, which could get slow as the site gets more use. $results = mysql_query($query); $response .= "<table><tr>"; if($nit == 0){ for($i = 0; $i < 60; $i++){ $dateCompare = date("Y-m-d", mktime(0,0,0,date("m"),date("d")+$i,date("Y"))); $dateForString = date("D d/m/Y", mktime(0,0,0,date("m"),date("d")+$i,date("Y"))); $response .= "<td height='350px' style='color:white; background-color:grey;'>$dateForString</td>"; while($row = mysql_fetch_array($results)){ $id = $row['id']; $poster = $row['poster']; $date = $row['date']; $response .= "$date : $dateCompare<br>"; if($date == $dateCompare){ $response .= "<td><a href='fullinfo.php?selected_id=$id'><img src='$poster' height='350px' /></a></td>"; } } } } FOLLOWED BY THE SECOND $response .= "<table><tr>"; if($nit == 0){ for($i = 0; $i < 60; $i++){ $dateCompare = date("Y-m-d", mktime(0,0,0,date("m"),date("d")+$i,date("Y"))); $dateForString = date("D d/m/Y", mktime(0,0,0,date("m"),date("d")+$i,date("Y"))); $response .= "<td height='350px' style='color:white; background-color:grey;'>$dateForString</td>"; $results = mysql_query($query); while($row = mysql_fetch_array($results)){ $id = $row['id']; $poster = $row['poster']; $date = $row['date']; if($date == $dateCompare){ $response .= "<td><a href='fullinfo.php?selected_id=$id'><img src='$poster' height='350px' /></a></td>"; } } } } First one results in -it seems - to only go through the while loop once, the first time. So out put is todays date, for the number of results from the database. The second works fine, prints out in order and matches up. I just don't like its inefficiency. Thanks Canty Hey, I have been having a problem with this code for a while now where I connect to the MySQL database to retrieve the desired information, but always get an error message. I have tried to correct it myself by changing bits of the code and searched the internet, but still have no idea how to fix it. Any help would be appreciated. The error is as follows: Notice: An error occured1: in <filelocation> on line 18 Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in <filelocation> on line 21 Notice: An error occured2: in <filelocation> on line 15 My code as is follows: require_once('../mysqli_connect.php') ; $query = "SELECT * FROM database WHERE mem_id='".($_GET['id'])."'"; $result = @mysql_query($dbc, $query) OR trigger_error("An error occured1: " . mysql_error()); $row = mysql_fetch_array($result) OR trigger_error("An error occured2: " . mysql_error()); echo "<b>Date: </b> ".$row['date']; echo "<br><b>ID: </b> ".$row['mem_id']; Thanks. I do a query and then do a while loop to go threw it, but later down the page I need to loop threw that data again, but nothing is there. Example while ($row = mysql_fetch_array($sqlmaps)) { //some code } //some code while ($row = mysql_fetch_array($sqlmaps)) { //it dose not work here now } Do need to do that exact same query 2 times to get the data? Hello if some one be nice to help me out it be most kind... i got this error "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in/home/23url/public_html/new.php on line 66" the code is - $getbans =mysql_query("SELECT banned FROM $tab[banned];"); $bans = array(); LINE 66 > while($ban=mysql_fetch_array($getbans)) { array_push($bans, $ban[0]); } many thanks Hi All, I am sorry to bother you but wondered if you might be able to spot my mistake... When I click on this page http://www.freepspwallpaper.co.uk/free-psp-wallpapers/free-babes-psp-wallpapers/ if returns an error as below (look carefully its hidden behind the header color). Im a bit of a newbie and cannot seem to remove it. Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/freepspw/public_html/dbconnect.php on line 22 Can anyone help? Peter I am having a problem with my script not returning anything! I echo the variable usually it will say ARRAY and it has something in it.. Nothing at all it coming up no errors I have put or die in it, nothing. I tried error_reporting(E_ALL); , still nothing. If someone can help me with this, I will greatly appreciate it. ~AJ error_reporting(E_ALL); //Get Catagorys and echo $getCatagorys = mysql_query("SELECT * FROM `catagory`") or die("Query failed with error: ".mysql_error()); while($row = mysql_fetch_array($getCatagorys, MYSQL_ASSOC)){ echo ' <tr> <td class="catagoryTD" width="550">'.$row['catagoryTitle'].'</td> <td class="catagoryTD" width="75"><center>Threads</center></td> <td class="catagoryTD" width="75"><center>Posts</center></td> <td class="catagoryTD" width="170"><center>Last Post</center></td> </tr>'; //Get SubCatagorys and echo $catagoryID = $row['catagoryID']; $getSubCatagory = mysql_query("SELECT * FROM `subcatagory` where catagoryID = '$catagoryID'"); while($subrow = mysql_fetch_array($getSubCatagory, MYSQL_ASSOC)){ $subCatagory = $subrow['subcatagoryID']; //Get Stats for number of threads $threads = mysql_query("SELECT * FROM entry WHERE subcatagoryID = '$subCatagory'") or die(mysql_error()); $num_threads = mysql_num_rows($threads); //Get Stats for number of entrys(posts) $posts = mysql_query("SELECT * FROM posts WHERE subcatagoryID = '$subCatagory'") or die(mysql_error()); $num_posts = mysql_num_rows($posts); //Find newest post and find what thread it is from $lastPost = mysql_query("SELECT * FROM `posts` ORDER BY `posts`.`postCreated` DESC") or die(mysql_error()); while($lastPostReturn = mysql_fetch_array($getSubCatagory, MYSQL_ASSOC)){ echo $lastPostReturn; } //Grab User and thread number and date $lastPostUser = $lastPostReturn['postUser']; $lastPostCreated = $lastPostReturn['postCreated']; $lastPostEntry = $lastPostReturn['entryID']; //Find the title of the thread and generate URL\ $lastPostThread = mysql_query("SELECT * FROM `entry` WHERE entryID = '$lastPostEntry'"); while($lastPostThreadReturn = mysql_fetch_array($lastPostThread, MYSQL_ASSOC)){ } echo ' <tr> <td><a href="catagory.php?id='.$subrow['subcatagoryID'].'">'.$subrow['subcatagoryTitle'].'</a></td> <td><center>'.$num_threads.'</center></td> <td><center>'.$num_posts.'</center></td> <td>'.$lastPostThreadReturn['text'].'<br>'.$lastPostCreated.'<br>'.$lastPostUser.'</td> </tr>'; } } Hey guys i have a query that select from multiple tables and works fine however i have the same field name in each table called:: member_id but i want to select specifically the member_id from table1 do i need to change the name when i loop through them? i really dont want to do that but if it confuses php unless is there a way when doin a while loop i specify what table i want it from? while($row = mysql_fetch_array($query)){ $member_id = $row['member_id']; } Hi I post this thread under php, because I guess that my coding is wrong. I always get the following message Quote Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/g/w/o/gwolff2005/html/admin/update.php on line 23 Update data in mysql The code is Code: [Select] <!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"> <?php $host="xxxx"; // Host name $username="xxxx"; // Mysql username $password="xxxxx; // Mysql password $db_name="xxxxx"; // Database name $tbl_name="sp_users"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // get value of id that sent from address bar $id=$_GET['user_id']; // Retrieve data from database $sql="SELECT * FROM '$sp_users' WHERE id='$user_id'"; $result=mysql_query($sql); $result=mysql_query("select * from '$sp_users' WHERE id='$user_id'"); $rows = mysql_fetch_array($result); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <form name="form1" method="post" action="update_ac.php"> <td> <table width="100%" border="0" cellspacing="1" cellpadding="0"> <tr> <td> </td> <td colspan="3"><div align="center" class="style1">Update data in mysql </div></td> </tr> <tr> <td align="center"> </td> <td align="center"> </td> <td align="center"> </td> <td align="center"> </td> </tr> <tr> <td align="center"> </td> <td align="center"><span class="style7">Name</span></td> <td align="center"><span class="style7">Lastname</span></td> <td align="center"><span class="style7">Email</span></td> </tr> <tr> <td> </td> <td align="center"><input name="name" type="text" id="name" value="<? echo $rows['user_first_name']; ?>"></td> <td align="center"><input name="lastname" type="text" id="lastname" value="<? echo $rows['user_surname']; ?>" size="15"></td> <td><input name="email" type="text" id="email" value="<? echo $rows['user_login']; ?>" size="15"></td> </tr> <tr> <td> </td> <td><input name="id" type="hidden" id="id" value="<? echo $rows['user_id']; ?>"></td> <td align="center"><input type="submit" name="Submit" value="Submit"></td> <td> </td> </tr> </table> </td> </form> </tr> </table> <? // close connection mysql_close(); ?> </body> </html>What am I doing wrong. Thanks for your help! please tell me the reason that why mysql_fetch_array does not work inside a function for example the following works well in its current form $sql="SELECT * FROM $tbl_13"; $query=mysql_query($sql); while($rs=mysql_fetch_array($query)){ $p0 = $rs["pfid"]; $pm = $rs["pm"]; } but when i put it inside a function it gives me an error at function defone() { $sql="SELECT * FROM $tbl_13"; $query=mysql_query($sql); while($rs=mysql_fetch_array($query)){ $p0 = $rs["pfid"]; $pm = $rs["pm"]; } return $pm; } the error message is... Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in... Please identify the problem.. i'm an experience programmer in ruby and perl but new in php |