for($i=0;$cast_row = mysql_fetch_array($res);$i++)
{
$cast['id'][] = $cast_row['user_id'];
$cast['role'][] = $cast_row['role'];
$cast['role_name'][] = $cast_row['role_name'];
$cast['is_approved'][] = $cast_row['is_approved'];
$cast['movie_id'][] = $cast_row['movie_id'];
}
for($i=0;$i<count($cast['id']);$i++) //LINE 31
{
$output .= "<tr>";
$mname = getMovieNameById($m_id);
$output .= "<td><a href='single.php?id=$m_id'>$mname</a></td>";
$aname = getArtistNameById($cast['id'][$i]);
$output .= "<td><a href=project.php?id={$cast['id'][$i]}>$aname</a></td>";
}
This code works fine in the web server but throws errors(notice) when executed on localhost
Notice: Undefined index: id in C:\wamp\www\Tinyflick\managemovie.php on line 31
What can be the problem? The rest of the code seems to work just fine
It kind of a silly mistake..
Its solved. Thanks to nico's comment
The error reporting level in localhost is different from the one on the server.
Changing that will do the trick.
The following code will show the all the errors and warnings
error_reporting(E_ALL)
Warnings are usually disabled on a production server. For more info refer the documentation of the said function
i guess the mysql result is just empty ! :)
try dumping the content of the row you get back.
btw, you could improove your code by doing something like this:
while($row = mysql_fetch_array($res)) { // iterates as long there is content
//do something with the $row... like your second for block! ;)
}
it turns out that $cast array is empty as you are not properly fetching the data from database.
if the error reporting is turned off in your server then you will not see any errors.
instead of for loop use the while loop, to iterate through the data fetched from database.
$cast = array();
while($row = mysql_fetch_array($res)) {
$cast['id'][] = $row['user_id'];
$cast['role'][] = $row['role'];
$cast['role_name'][] = $row['role_name'];
$cast['is_approved'][] = $row['is_approved'];
$cast['movie_id'][] = $row['movie_id'];
}
and then you can run the for loop.
I guess that if you want supress those notices, add the following line before the loop:
$cast = array('id'=>array(), 'role'=>array(), 'role_name'=>array(), 'is_approved'=>array(), 'movie_id'=>array() );
to initialize the variable.
Related
I'm having a bit of a problem with sqlite on my website and I cannot figure out what I'm doing wrong. I am trying to retrieve content from a row in an sqlite database, which works, but it is stored as a "object" variable. I am trying to convert it to a php "array()". I have tried several ways of doing this but all have failed. This is very frustrating and was wondering if anyone could be kind enough to help. The reason I need this done is so that I can compare 2 arrays to see if there are any matching texts in either of them.
Here is my current code:
include("template.php");
// Create (connect to) SQLite database in file
$file_db = new PDO('sqlite:Scrambled/data.sqlite');
// Set errormode to exceptions
$file_db->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
$result = $file_db->query('SELECT * FROM users');
//$results = array(mysql_fetch_assoc($result));
$resultslist = array();
while(($row = mysql_fetch_assoc($result))) {
$resultslist[$row] = $row['users'];
}
$qualifiedpeople = explode("\n", file_get_contents('Scrambled/roboticsmembers.txt'));
//$lol = array(fetch_assoc($qualifiedpeople));
$members = array_intersect($qualifiedpeople, $resultslist);
echo("$resultslist");
echo("$qualifiedpeople");
//foreach($members as $member) {
//echo("$member \n");
//}
As you can see I have tried different things and some lines are commented out for later reference.
You can see the error right here: citadelwars.net/members.php
"Warning: mysql_fetch_assoc() expects parameter 1 to be resource, object given in C:\wamp\www\members.php on line 12"
I have not found this error in all of my googling, but I'm sure it means that the pdo is giving me an "object" variable when it needs to be a resource. Can you tell me if there's any way around fixing this?
I've found the solution and what I did wrong;
I was able to replace:
while(($row = mysql_fetch_assoc($result))) {
$resultslist[$row] = $row['users'];
}
With this:
$i = 0;
foreach($result as $row) {
$resultslist[$i] = $row['name'];
$i++;
}
Which worked perfectly.
Also for comparing the tables I replaced explode("\n", with explode("\r\n",
I would like to create a simple select drop down, to be populated by a table in my MYSQL database.
Here is my code:
$q = 'SELECT * FROM Shipmethods';
$result = mysqli_query($connection, $q);
echo '<select name="shipmethod">';
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC));
{
echo '<option value="' . $row['shipmethodid'] . '">' . $row['shipmethoddesc'] . '</option>';
}
echo '</select>';
mysqli_free_result($result); // free up the results
}
There is no connection error, no typos in table name or column names. The HTML output shows that a select option exists, but it is empty. I would like to use mysqli for this.
There seem to be countless topics already on this subject, but as far as I can see, my code matches the correct answers of those already answered questions.
Thanks in advance.
Remove ; from this line, rest code seems fine to me, let me know if it works or not --
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC));
To
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
First off, based on the evidence we have, the most likely issue, other than an empty result set, is the issue that swapnesh identified, which is clearly a logic error. I'm providing this illustration, which simulates the problem to make it clear that it is NOT a syntax error to do what was accidently done here. The misplaced semicolon in essence causes the following block to be entered only after the while loop has been exhausted.
Consider this example:
<?php
$r = array(1,2,3,4);
function fetchit(&$r) {
return array_pop($r);
}
while ($val = fetchit($r))
{
echo $val;
}
This would be the intended use. Now what happens if the script has this code instead?
<?php
$r = array(1,2,3,4);
function fetchit(&$r) {
return array_pop($r);
}
while ($val = fetchit($r));
{
echo $val;
}
Trying these you will see that both scripts run without error. Although unnecessary, php does not care if you randomly include an uneeded block somewhere in your script { }, and it also does not care if an if, while, for etc, is followed by a block. It's in fact common practice for people to shorten up code with:
if (//somecondition)
statement;
Many people frown on that practice but it's legal and programmers use it all the time.
UPDATE: I solved it using a for loop:
for ($i=0; $i < mysql_num_rows($result); $i++) {
$row = mysql_fetch_assoc($result);
echo $row['name'];
}
ORIGINAL QUESTION:
This looks kinda stupid. I'm sure im missing something that's very simple, since I was able to accomplish this before. Anyways, I want to echo some text for every item in an array. This array is derived from mySQL.
here's the code
while ($row = mysql_fetch_assoc(mysql_query("SELECT * FROM files"))) {
echo $row['name'];
}
can you post the complete code? I think you forgot the database connection.
Try this:
$result = mysql_query("SELECT * FROM files") or die (mysql_error());
while ($row = mysql_fetch_assoc($result)) {
var_dump($row['name']);
}
This will throw an error, I guess you made a mistake over there. Also, var_dump() your $row in the while to make 100% sure you have "a" value.
Also, are you sure the row does exist? If don't have any records, the echo on your $row will not work sinc it does not exist.
Also, set error reporting to E_ALL like so.
error_reporting(E_ALL);
Also, since you are running your query inside the while() loop, it will continue to run forever. So first run the query, and put it in a variable, and then loop through the results. (see my piece of code above)
You can execute query individual instead of while loop because if your query return more than 1 rows it will goes under the loop. show your loop print only first data of result and your loop is infinite.
From your question it seems so simple, try this way it's working.
$sql="SELECT name From files";
$names = $db->query($sql);
while($name1 = $db->fetchByAssoc($names))
{
echo $name1['name'];
}
Okay, so i have created a new support ticket system, but in my ticket search page it keeps giving me errors like undefined variable in line 197. the weird thing is that the variable is defined right above it. Please assist me in this here is a link to the code: http://pastebin.com/AMzRLDK4
I'm trying to make it possible for me to view the support tickets that are open and mark them as read or change the status and to reply to them by going to the pm system. I had it working last night but i must have changed something without realizing its effect.
Thanks in advance,
Matt.
It looks like this is the first time you use $Sid or $Sname in your code. They are inside a code block for the while, which means that is the only place they exist. Also, I think you want to use mysql_fetch_assoc(). It'll actually work with the column names, instead of the indexes. (And probably best off to use the newer MySQLi for several reasons)
while($raw = mysql_fetch_array($ret)){ $Sid = $raw['id']; $Sname = $raw['username']; }
Quick Fix:
$Sid = null; //or 0 whichever makes sense for you
$Sname = null; //or ''
while($raw = mysql_fetch_assoc($ret)){ $Sid = $raw['id']; $Sname = $raw['username']; }
However, with the LIMIT 1 in the MySQL Query, you could drop the WHILE all together
$raw = mysql_fetch_assoc($ret);
if($raw === false)
{
//Error Condition
}
$Sid = $raw['id'];
$Sname = $raw['username'];
I'm making a website using php and I'm having some trouble with an SQL query.
$dataArray = array();
$result = mysql_query("SELECT * FROM test WHERE web_id='$websiteID'")
or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$k = $row['kfoo'];
$v = $row['vbar'];
$dataArray[$k] = $v;
}
That's the code as it should be, however that causes the page to go blank (i.e. simply display all white). I've double checked and web_id is definitely the correct name and the correct case.
If I change web_id in the query to be, for instance, 'foo' (basically anything other than web_id), then the page will display, but with the error message "Unknown column 'foo' in 'where clause'".
Does anybody have any idea what could be causing this? Here's the code where I create the table test:
$dataDB = "CREATE TABLE test
(
data_id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(data_id),
web_id INT,
kfoo TEXT,
vbar TEXT
)";
mysql_query($dataDB,$con);
Update
Based on some of the answers here, I removed the quotes from around $websiteID and displayed errors using
error_reporting(E_ALL);
ini_set('display_errors', 1);
This displayed a lot of errors, including a syntax error earlier on that I've now fixed on. However, many errors remain and they don't make much sense to me. Here's the full code for my method:
function getOutputSQL($websiteID,$userInput) {
$result = mysql_query("SELECT * FROM websites WHERE websiteID=$websiteID")
or die(mysql_error());
while ($row = mysql_fetch_array($result)){
$url = $row['url'];
$exp = $row['exp'];
$output= $row['textPrint'];
$endUrlStart = $row['outUrlStart'];
$endUrlEnd = $row['outURLEnd'];
$image = $row['image'];
$print = $row['print'];
$post = $row['post'];
$dataSource = $row['dataSource'];
$dataSourceName = $row['dataSourceName'];
}
// construct array of data names and values
$dataArray = array();
$result = mysql_query("SELECT * FROM test WHERE web_id=$websiteID")
or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$k = $row['kfoo'];
$v = $row['vbar'];
$dataArray[$k] = $v;
}
// construct array of expressions and replacements
$result = mysql_query("SELECT * FROM regex WHERE web_id=$websiteID");
$expArray = array();
while ($row = mysql_fetch_array($result)) {
$e = $row['ex'];
$r = $row['re'];
$expArray[$e] = $r;
}
return getOutput($userInput,$url,$exp,$output,$endUrlStart,
$endUrlEnd,$dataSource,$dataSourceName,$post,$image,$print,
$expArray,$dataArray);
}
The errors I'm getting are all like this -
Notice: Undefined variable: output in /home/daniel/web/resultsTest.php on line 113"
That repeats several times for url, exp, output, endUrlStart, endUrlEnd, dataSource, dataSourceName, post, image and print Line 113 is the large return line.
The thing is, as far as I can tell these variables are defined, and I know the table isn't empty, because I can display it on another page.
Solved
Sorted. The problem was actually in another part of my code - I was calling getOutputSQL incorrectly, but I've fixed it now!
I expect that your actual code actually does out put anything, If this is all the code you have it makes sence nothing is outputted since you don't when there is no error (no echo's or prints are templates or anything)
Also make sure you set both error_reporting and display_errors are set. You can do this from the top of your script using
error_reporting(E_ALL);
ini_set('display_errors', 1);
That way uou'll see all errors.
EDIT
PHP has multiple types of errors, including fatal errors, warnings and notices. Notices tell you when your using undefined variables or deprecated functions, you can pretty much ignore them. You can change the setting so you won't see them using:
error_reporting(E_ALL ^ E_NOTICE);
Then you will only see real errors (warnings and fatal errors).
More Edit
Then those variables are truly unset, try something like this before you call getOutput():
echo "<pre>";
var_dump($userInput);
var_dump($url);
//etc.
echo "</pre>";
and see if the variables are really set.
In general a blank page in php indicates disabled error notifications. At first you should enable them via .htaccess or php.ini.
BTW: As far as I know integers shouldn't be quoted in SQL.
Try this instead:
$dataArray = array();
if(is_numeric($websiteID){
$result = mysql_query("SELECT * FROM test WHERE web_id=$websiteID") or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$k = $row['kfoo'];
$v = $row['vbar'];
$dataArray[$k] = $v;
}
}
Notice I removed the '' around the websiteID variable. This means mysql won't treat it as a string but as an int, which is what you specified in the mysql table creation. I also check to see if the websiteID is a number. If it is not, then there is no point in doing the sql query, as you will get zero results. This protects against sql injections.
Variables you define inside loops are destroyed at the end of the loop. That means the output variable you create in the first while loop does not exist after the loop. You should define them outside the while loop and then set the values inside the loop:
function getOutputSQL($websiteID,$userInput) {
$result = mysql_query("SELECT * FROM websites WHERE websiteID=$websiteID")
or die(mysql_error());
$url = "";
$exp = "";
$output = "";
//... etc for all the other variables you need in the function at the end.
while ($row = mysql_fetch_array($result)){
$url = $row['url'];
$exp = $row['exp'];
$output= $row['textPrint'];
$endUrlStart = $row['outUrlStart'];
$endUrlEnd = $row['outURLEnd'];
$image = $row['image'];
$print = $row['print'];
$post = $row['post'];
$dataSource = $row['dataSource'];
$dataSourceName = $row['dataSourceName'];
}
// the rest of your function...