Jump to content

malikah

Members
  • Posts

    87
  • Joined

  • Last visited

    Never

Everything posted by malikah

  1. Hi, does anyone know where I'm going wrong here? $name = ""; $email = ""; $subject = ""; $message = ""; $fieldVals = array($name, $email, $subject, $message); foreach($fieldVals as $key => $val) $fieldVals[$key] = "abc"; echo $name; echo $email; echo $subject; echo $message; //output: <nothing> (I was hoping to see "abc" for each variable). Cheers.
  2. I'm trying to link to a page that takes its own parameter and then checks that parameter to open another page which also has its own parameters: href='blah.php?page=view.php?a=apple&b=ball&c=car' And of course this isn't working - any ideas how I can get this to work? Cheers.
  3. You mean this? $private $conn; function __construct() { $this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME); }
  4. I'm trying to create an associative array: $valsOne = array("A","B","C","D","E"); $valsTwo = array("apple","orange","grape","kiwi","banana"); for($i=0;$i<sizeof($valsOne);$i++) { $bigArray[$i] = $valsOne[$i]; $bigArray[$valsOne[$i]] = $valsTwo[$i]; } foreach($bigArray as $key => $value) echo $key . "=" . $value . "<br />"; I was hoping that the output would show the letter and then the fruit, but instead I get this: 0=A A=apple 1=B B=orange 2=C C=grape 3=D D=kiwi 4=E E=banana What am I doing wrong?
  5. This worked for me procedurally: $query = "SELECT COLUMN_NAME FROM information_schema.COLUMNS where table_name='table'"; if(mysqli_real_query($dblink, $query)) { $result = mysqli_store_result($dblink); while($row = mysqli_fetch_assoc($result)) { foreach($row as $_colName => $_colValue) { echo "{$_colValue}<br />"; } } } Unfortunately, I can't figure out the object-oriented equivalent. Here's my attempt: $query = "SELECT COLUMN_NAME FROM information_schema.COLUMNS where table_name='table'"; if($stmt = $this->conn->prepare($query)) { $stmt->execute(); $stmt->store_result(); while($row = $stmt->fetch()) { foreach($row as $_colName => $_colValue) { echo "{$_colValue}<br />"; } } }
  6. OK, I used "DESCRIBE table" to get it working: $query = "DESCRIBE table"; if($stmt = $this->conn->prepare($query)) { $stmt->execute(); $stmt->store_result(); echo $stmt->num_rows(); $stmt->close(); } NOW.. All I need is to return the names of each field...
  7. So far this works for ROWS but I need something for COLUMNS: $query = "SELECT * FROM table"; if($stmt = $this->conn->prepare($query)) { $stmt->execute(); $stmt->store_result(); echo $stmt->num_rows(); $stmt->close(); }
  8. I'm actually trying to count columns, not rows - but thanks
  9. Hi, I'm trying to return the name of each column in a MySQL database using PREPARED statements only. Cheers.
  10. I tried that, but unfortunately get the error: "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource".
  11. Hi, I'm having trouble returning a full row count from my database. The following code returns "1" column instead of "24" columns. $conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) $query = "SELECT * FROM table"; if($stmt = $this->conn->prepare($query)) { $stmt->execute(); if($stmt->fetch()) { if ($stmt->field_count) { $result = $stmt->store_result(); } $stmt->close(); return $result; } }
  12. OK, figured it out: I changed: $stmt->bind_param($esses, $newValues); To: eval("\$stmt->bind_param(\$esses," . $newValues . ");"); Works flawlessly..
  13. OK, here's my function with sample arrays (the real arrays are much larger): $columns = array("name","address","phone","company"); $values = array("Jon","123 here st","1234567890","company Inc."); $numberOfEntries = sizeof($values); employerSignUpForm($columns, $values, $numberOfEntries); function employerSignUpForm($columns, $values, $numberOfEntries) { $questionMarks = array(); $esses = ""; $columnNames = implode(',',$columns); $newValues = array(); for( $i=0; $i<$numberOfEntries; $i++ ) { $questionMarks[$i] = "?"; $esses .= "s"; $$columns[$i] = $values[$i]; $newValues[$i] = "$".$columns[$i]; } $newValues = implode(',',$newValues); $questionMarks = implode(',', $questionMarks); $query = "INSERT INTO userinfo ($columnNames) VALUES ($questionMarks)"; if($stmt = $this->conn->prepare($query)) { $stmt->bind_param($esses, $newValues); $stmt->execute(); $stmt->close(); return true; } } This will lead to the error: "...Number of elements in type definition string doesn't match number of bind variables..."
  14. You either need actual variables and use eval() to build the line of php code or you cannot use prepared statements to do what you are trying to do. I've managed to echo any one of the individual $variables values, but for some reason the prepared statement sees $values as a single value and not as a list of variables. How do you suggest using eval() in this situation?
  15. I still don't get it. I've been here: http://us3.php.net/manual/en/function.eval.php but the examples don't apply to my situation. All I want to do is insert an array of values into a database using a prepared statement. Surely there's an easy way? Cheers.
  16. Hi. I've been searching for days on how to insert an array of values using a prepared statement. If you look in particular at $stmt->bind_param($esses, $values); this is where the problem is ($values). I get the error: "Number of elements in type definition string doesn't match number of bind variables" I know this is where the problem is because if I use a comma-separated list it works fine. How on earth can I just use the array passed through the function? $columns = array("a", "b", "c", "d", "e", "f", "g"); $values = array("1","2","3","4","5","6","7"); $numberOfEntries = sizeof($values); insertData($columns, $values, $numberOfEntries); function insertData($columns, $values, $numberOfEntries) { $questionMarks = array(); $esses = ""; $columnNames = implode(',',$columns); for( $i=0; $i<$numberOfEntries; $i++ ) { $questionMarks[$i] = "?"; $esses .= "s"; } $questionMarks = implode(',', $questionMarks); $query = "INSERT INTO theTable ($columnNames) VALUES ($questionMarks)"; if($stmt = $this->conn->prepare($query)) { $stmt->bind_param($esses, $values); $stmt->execute(); $stmt->close(); return true; } }
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.