-
Posts
3,145 -
Joined
-
Last visited
-
Days Won
37
Everything posted by cyberRobot
-
how do I count rows for a particular column in this query?
cyberRobot replied to kaiman's topic in PHP Coding Help
Using the mysql_num_rows() function should work if you do something like this. ... // select info from comments database $result3 = mysql_query ("SELECT count(*) FROM $table_name3 WHERE id='$post_id' ORDER BY id DESC LIMIT 1") or trigger_error("A mysql error has occurred!"); if (mysql_num_rows($result3) > 0 ) { // display number of comments echo 'Number of comments: ' . mysql_num_rows($result3); while($row = mysql_fetch_array($result3)) { extract($row); ... Note that you'll probably need to remove the "LIMIT 1" part since you'll only get one result every time. You probably also don't need the count() function in the query. -
You should be able to break apart the URL with explode(): //GET THE CURRENT URL $url = $_SERVER['PHP_SELF']; //BREAK APART THE URL $urlPieces = explode('/', $url); //LOOP THROUGH THE URL PIECES foreach($urlPieces as $currPiece) { echo "<li>$currPiece</li>"; } You'll need to replace the echo statement in the foreach loop with whatever formatting you need to get the search query. Then just redirect the page.
-
No problem, glad everything worked out in the end.
-
Grade and comments don't work due to the incorrect variable names. ($grade vs $esc_grade) ... $grade = $_POST['grade']; $comments = $_POST['comments']; ... $sql = "INSERT INTO student_course (SID, CID, GRADE, COMMENTS) VALUES ('{$sid}', '{$cid}', '{$esc_grade}', '{$esc_comments}')"; Of course those variable names would work if keep the two mysql_real_escape_string() statements. ... $esc_grade = mysql_real_escape_string($grade, $this->conn); $esc_comments = mysql_real_escape_string($comments, $this->conn); ...
-
Could you try posting your current code again (function to insert the row and the code that calls the function)?
-
I would if you don't plan to open multiple database. It's one less thing you need to type.
-
With this script, are you planning to open multiple databases? If not, the database connection reference ($this->con) isn't required. If the reference isn't there, PHP defaults to the last opened database. For example see: http://php.net/manual/en/function.mysql-query.php
-
Hmmm...with it being in a class "$this->conn" should work.
-
So you changed the "$this->conn" to "$db1->conn"? Is the function inside a class object?
-
Seems kind of weird that the "$this" reference stopped working after adding the echo statement. Did you change anything else? I'm just guessing, but does it work if you change the "$this->" references to "$db1->"?
-
Did you insert the echo statement into your function? function insert_student_course($sid, $cid, $grade, $comments) { $esc_grade = mysql_real_escape_string($grade, $this->conn); $esc_comments = mysql_real_escape_string($comments, $this->conn); $sql = "INSERT INTO student_course (SID, CID, GRADE, COMMENTS) VALUES ('{$sid}', '{$cid}', '{$esc_grade}', '{$esc_comments}')"; echo $sql; $result = mysql_query($sql, $this->conn); if (!$result) { die("SQL Insertion error: " . mysql_error()); } else { $numofrows = mysql_affected_rows($this->conn); return $numofrows; } }
-
If you try this, what does it display: $sql = "INSERT INTO student_course (SID, CID, GRADE, COMMENTS) VALUES ('{$sid}', '{$cid}', '{$esc_grade}', '{$esc_comments}')"; echo $sql; $result = mysql_query($sql, $this->conn);
-
Since the result set is being saved to $result1, you need to change the while loop while ($row = mysql_fetch_array($result1)) And I think the field name case needs to match here: $sql = "SELECT sid FROM STUDENTS"; and here: echo "<option value='{$row['SID']}'>{$row['SID']}</option>";
-
Could you post your updated code (drop-down code and getResult function)? Also, when you say you have an empty drop-down menu. Does it look like there are 34 blank spots where the data should be?
-
Did you try both suggestions together? while ($row = mysql_fetch_array($result)) echo "<option value='{$row['SID']}'>{$row['SID']}</option>";
-
Maybe change to: while ($row = mysql_fetch_array($result)) With mysql_fetch_row() you would need to refer to the array as $row[0]
-
No problem As far as I'm aware, it doesn't matter how you use case in your tables. As long as your code refers to the fields as they are in the table, you should be fine. Note that I don't know what "PK" and "FK" means. So you're getting data... Speaking of case, does this do the trick: echo "<option value='{$row['SID']}'>{$row['SID']}</option>"; If that doesn't work, maybe you need to remove the single quote: echo "<option value='$row[sid]'>$row[sid]</option>"; echo "<option value='$row[sID]'>$row[sID]</option>";
-
Sorry, I just noticed that the above code refers to two seperate tables. So that probably isn't the problem. Do you know if the query is returning results? What do you get if you do something like this: $sql = "SELECT SID FROM STUDENTS"; $result = $db1->getResult($sql); print mysql_num_rows($result);
-
The other thing that might be throwing things off is the field name. In the MySQL table is the field name "SID" or "sid"? In the original post you use both upper and lower case fieldnames. $sql = "SELECT SID FROM STUDENTS"; ... $sql = "INSERT INTO student_course (sid, cid, grade, comments) VALUES ('{$sid}', '{$cid}', '{$esc_grade}', '{$esc_comments}')"; Also, I'm a little confused by what you mean by the drop-down list is now showing. What is the difference between these two statements? Most recent statement: "Now it shows the drop down list. But the list is empty!" Earlier statement: "The drop down menu show, but I can not see the options, to choose them."
-
I think the issue is that your function "getResult()" is returning the number of results found instead of the actual result set. Does it work if you change the function to: function getResult($sql) { $getResult = mysql_query($sql, $this->conn); if (!getResult) { die("SQL Insertion error: " . mysql_error()); } else { return $getResult; } }
-
What kind of error are you getting? Is the drop-down menu not showing up?
-
That's not exactly true. Tables have not been depreciated, they are still need for data tables.
-
Performing case-INsensitive searchs against a latin1_general_cs field
cyberRobot replied to cyberRobot's topic in MySQL Help
Thanks! -
I have a username field which is set to latin1_general_cs. Is there a way to search the field so that it doesn't look at the case? For example, if there is already a username for "jsmith" I want to prevent the creation of usernames like "JSmith". Note that I want to keep the field set to latin1_general_cs. Here is my current query for reference: $sql = "SELECT id FROM members WHERE id!=$id AND username='" . mysql_real_escape_string($username) . "'";
-
Have you tried using something like Google Analytics?