Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Function Variables Advice Needed
    text
    copied!<p>I have a simple form that I am passing data from to a php page using session variables. I have two functions in an external function page, but I cannot get the variables in each function to work globally unless I name the variable in each particular function.</p> <p>Here is an example that I am hoping someone can help with.</p> <pre><code>$dbName = $_SESSION['dataBaseName']; $tableName = $_SESSION['tableName']; $artistName = $_SESSION['artistName']; $songName = $_SESSION['songName']; function table_exists($tableName) { if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$tableName."'"))==1) { echo "Table exists &lt;br /&gt;"; } else { $sql = "CREATE TABLE songList (songId int NOT NULL AUTO_INCREMENT PRIMARY KEY, artistName varchar(60), songName varchar(25))"; $result= mysql_query($sql) or die(mysql_error()); return $result; } } </code></pre> <p>In my OTHER function I put the variables INSIDE the function. Doesnt seem to matter what combo I use, my variables have to be REDECLARED every function I use. I tried using GLOBAL in front but that just causes an error. Here is the other function:</p> <pre><code>function tableWrite() { $dbName = $_SESSION['dataBaseName']; $tableName = $_SESSION['tableName']; $artist = $_SESSION['artist']; $song = $_SESSION['song']; if(!empty($_REQUEST['insert'])) { $sql = "INSERT INTO $tableName (artistName, songName) values ('$artist', '$song')"; $result = mysql_query($sql) or die(mysql_error()); $showaresult = mysql_query("SELECT * from $tableName where artistName = '$artist' AND songName= '$song' ") or die("Invalid query: " . mysql_error()); echo ("New entry added"); $row = mysql_fetch_array($showaresult); echo ("&lt;br&gt; Catalog Number = ". $row["songId"] . "&lt;br&gt; Artist Name = " . $row["artistName"] . "&lt;br&gt;"); echo("Song Name = " . $row["songName"] . "&lt;br&gt;"); echo ("&lt;h1&gt; Current Catalog &lt;/h1&gt;"); $showresult = mysql_query("SELECT * from $tableName") or die("Invalid query: " . mysql_error()); while ($row = mysql_fetch_array($showresult)) { echo ("&lt;br&gt; Catalog Number = ". $row["songId"] . "&lt;br&gt; Artist Name = " . $row["artistName"] . "&lt;br&gt;"); echo("Song Name = " . $row["songName"] . "&lt;br&gt;"); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload